欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android給通知channel靜音的方法實(shí)例

 更新時間:2022年02月10日 12:02:05   作者:BennuCTec  
這篇文章主要給大家介紹了關(guān)于Android如何給通知channel靜音的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

前言

目前各個市場都要求targetsdkversion要不低于26,也就是android 8.0。 相應(yīng)的影響很多功能,比如通知。

當(dāng)targetsdkversion >= 26,需要為通知添加channel,如

manager = (NotificationManager) BaseApp.getAppContext()
        .getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    NotificationChannel channel = new NotificationChannel("update", "update", NotificationManager.IMPORTANCE_DEFAULT);
    manager.createNotificationChannel(channel);
}
builder = new NotificationCompat.Builder(BaseApp.getAppContext(), "update");

首先要新建一個NotificationChannel,并調(diào)用NotificationManager的createNotificationChannel啟用該通道。

然后在創(chuàng)建buidler的時候設(shè)置同樣的channelid即可。

靜音

那么如果想讓通知靜音怎么處理?

  • 可以在channel上設(shè)置,setSound(null, null)。注意如果已經(jīng)安裝且這個channel已經(jīng)存在,再覆蓋安裝不能生效,需要卸載重裝。
  • 也可以對builder進(jìn)行設(shè)置,setOnlyAlertOnce(true)。注意這個并不是完全靜音,而是讓這個通知只響一次,比如顯示下載進(jìn)度時,就不會一直響。
manager = (NotificationManager) BaseApp.getAppContext()
        .getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    NotificationChannel channel = new NotificationChannel("update", "update", NotificationManager.IMPORTANCE_DEFAULT);
    channel.setSound(null, null);
    manager.createNotificationChannel(channel);
}
builder = new NotificationCompat.Builder(BaseApp.getAppContext(), "update");
...
builder.setOnlyAlertOnce(true);

總結(jié)

到此這篇關(guān)于Android給通知channel靜音的文章就介紹到這了,更多相關(guān)Android給通知channel靜音內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論