通知的基本用法

2021-08-21 21:08:46 字數 2554 閱讀 3001

通知是android系統比較乙個有特色的功能,當應用程式需要向使用者傳送乙個提醒時,就可以借助通知來實現,大部分android使用者都喜歡這個功能,就連ios也就5.0版本之後加入了類似功能

1、需要乙個notificationmanager來對通知進行管理,可以呼叫content的getsystemservice()方法獲取到,getsystemservrce方法接受乙個字串引數用於確定獲取系統的那個服務,這裡我們傳入content.notification_servrce,就可以寫成下面這樣

nofiticationmanager manager = (notificationmanager)

getsystemservice(content.notification_servrce);

//為了在所有android系統版本都可以執行,新增如下**:

nofitication nofitication = new notificationcompat.builder(content).bulid

;

2、notification的基本設定

nofitication nofitication = new notificationcompat.builder(content)

.setcontenttitle("這裡設定標題")

.setcontenttext("這裡是正文內容")

.setwhen(system.currenttimemillis())//這裡設定顯示時間

.setsmallicon(r.drawable

.small_icon)//這裡設定小圖示

.setautocancel(true)//取消顯示

.setlargeicon(bitmapfactory.decoderesource(getresources(),r.drwable

.large_icon))

.bulid

;

3、顯示通知

manager.notify(1,notification);//第乙個引數是id,第二個引數則是notification物件
4、通知的高階技巧

.setsound(uri.frommfile()new file("路徑"))

.setvibrate(new

long)//下標0為靜止時長,下標1為振動時長,下標2為靜止時長,以此類推,控制手機振動需要宣告許可權

"android.permission.vibrate"/>

.setlights(color.green,1000,1000)//設定led燈的顏色,第二個引數設定亮起的時長,第三個引數設定暗下的時長,通知來臨

//時就是一閃一閃的效果

.setdefaults(notificationcompat.default_all)//預設設定

5、通知的高階功能

.setcontenttext("當文字內容過多,通知欄無法顯示的時候,多餘部分用省略號代替")

.setstyle(new notificationcompat.bigtextstyle().bigtext("這樣就可以全部顯示了"))

.setstyle(new notificationcompat.bigpicturestyle().bigpicture(bitmapfactory.decoderesource(getresources(),r.drawable

.big_image)))//設定顯示

6、通知的重要程度

//一共五個值

priorty_default 預設

priority_min 最低

priority_low 較低

priority_higr 較高

priority_max 最高

.setpriority(notificationcompat.priority_max)

intent intent = new intent("android.media.action.image_capture");

intent.putextra(meidastore.extra_output,imageuri);

startactivityforresult(intent,take_photo)

intent intent = new intent("android.intent.action.get_content");

intent.settype("image/*");

startactivityforresult(intnet,choose_photo);

trycatch(exception e)
file file = new file(environment.getexternalstoragedirectory(),"檔名.副檔名");

videoview.setvideopath(file.getpath());//路徑

通知協議KVO的用法

當我們第一次編寫ios應用時,我們注意到不斷的在使用 delegate 並且貫穿於整個sdk。delegation模式不是ios特有的模式,而是依賴與你過去擁有的程式設計背景。針對它的優勢以及為什麼經常使用到,這種模式可能不是很明顯的。delegation的基本特徵是,乙個controller定義了...

通知(NSNotification)的基本學習

通知 觀察a,如果a發生變化,需要b去做一些事情,就給b註冊觀察者,分兩種情況 第一種,不傳遞引數,只是通知b,a發生了變化 a的 nsnotificationcenter defaultcenter postnotificationname change object nil b的 nsnotif...

swift 通知的基本使用

通知的使用 1.發通知。以這條通知為例,通知名字 nicknamenotification 通知引數 title nsnotificationcenter.defaultcenter postnotificationname nicknamenotification object title 此處的...