Android 8 1 通知的變化

2021-09-11 16:17:48 字數 2719 閱讀 7851

android 7.0 通知的寫法如下:

notificationmanager manager=(notificationmanager)getsystemservice(notification_service);

notification notification=new notificationcompat.builder(mainactivity.this)

.setcontenttext("content ")//設定通知內容

.setcontenttitle("content title")//設定通知標題

.setlargeicon(bitmapfactory.decoderesource(getresources()

,r.mipmap.ic_launcher))

.setsmallicon(r.mipmap.ic_launcher)

.build();

manager.notify(1,notification);

這是7.0通知的基本寫法,如果需要加入震動,led燈 的閃爍,音效等只需要加在build()之前即可,如:

notificationmanager manager=(notificationmanager)getsystemservice(notification_service);

notification notification=new notificationcompat.builder(mainactivity.this)

.setcontenttext("content ")//設定通知內容

.setcontenttitle("content title")//設定通知標題

.setlargeicon(bitmapfactory.decoderesource(getresources()

,r.mipmap.ic_launcher))

.setsmallicon(r.mipmap.ic_launcher)

.setlights(color.green,1000,1000)

.setsound(uri.fromfile(new file("音訊檔案位置")))

.setvibrate(new long)

.build();

manager.notify(1,notification);

新增led燈閃爍,第乙個1000是亮的ms,第二個是熄滅的ms,也就是亮一秒,滅一秒(綠色的燈)。                       

.setlights(color.green,1000,1000)

新增聲音 如果你是按照書上的路徑,有可能你沒有此檔案,實驗的最好辦法就是新增自己的路徑。

.setsound(uri.fromfile(new file("音訊檔案位置"))

新增震動:陣列中奇數字置是不震動的ms,偶數字是震動的ms,需要注意的是震動是需要許可權的,所以需要在manifest中新增:

.setvibrate(new long)//此不需要新增

android 8.1 (可能8.0等也適用,因為我手機是8.1)

string id="channel_1";

notificationmanager manager=(notificationmanager)getsystemservice(notification_service);

//配置通道

notificationchannel channel = new notificationchannel(id,"my_channel",notificationmanager.importance_default);

channel.enablevibration(true);

channel.setvibrationpattern(new long);

string path="android.resource:"+r.raw.music;

channel.setsound(uri.parse(path),notification.audio_attributes_default);

manager.createnotificationchannel(channel);

notification notification=new notificationcompat.builder(mainactivity.this,id)

.setcontenttext("content")

.setcontenttitle("content title")

.setlargeicon(bitmapfactory.decoderesource(getresources()

,r.drawable.timg))

.setsmallicon(r.mipmap.ic_launcher_round)

.build();

manager.notify(1,notification);

老版本的android因為通知的不安全性,所以加入了通道這一引數,可以看到:

notification notification=new notificationcompat.builder(mainactivity.this,id)

id就是指定的通道,在此之上配置了乙個叫channel_1的通道,同樣震動,音訊也是在通道裡設定,而不是像以前一樣,至於led燈,可能是手機問題,我的手機沒有實現,音訊的路徑是我自己新增的,不是系統的音訊路徑,所以需要自己改,至於pendingintent等功能沒有變化。

Andoid8 0通知機制變化

需要說明一點 notificationchannel不同使用者下是通過uid來區分的,所以存在多使用者時系統在尋找channel時是以pkg uid為key來進行查詢的。在應用啟動時,需要建立兩個通知管道,並對管道設定一些初始狀態,包括管道名稱 鈴聲 振動 led提醒 圓點通知等,如下所示,為新資訊...

Android 8 0 通知相容

android 8.0 引入了通知渠道,其允許您為要顯示的每種通知型別建立使用者可自定義的渠道。使用者介面將通知渠道稱之為通知類別。targesdk公升級到26之後,所有的通知的實現都需要提供通知渠道,如果不提供通知渠道的話,所有通知在8.0系統上面都不能正常展示,下面來看 既然是相容,當然不能少了...

Android 7 0 通知新特性

markdown 是一種輕量級標記語言,它允許人們使用易讀易寫的純文字格式編寫文件,然後轉換成格式豐富的html頁面。維基百科 使用簡單的符號標識不同的標題,將某些文字標記為粗體或者斜體,建立乙個鏈結等,詳細語法參考幫助?本編輯器支援markdown extra,擴充套件了很多好用的功能。具體請參考...