溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android中NotificationListenerService怎么用

發(fā)布時間:2021-08-09 11:04:22 來源:億速云 閱讀:137 作者:小新 欄目:移動開發(fā)

這篇文章主要介紹了Android中NotificationListenerService怎么用,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

使用

新建一服務(wù)類,使它繼承NotificationListenerService,并實現(xiàn)兩個重要的方法:

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) 
public class NotificationListener extends NotificationListenerService { 
  privatestatic final String TAG = "NotificationListener"; 
  
  @Override 
  public void onNotificationRemoved(StatusBarNotification sbn) { 
    Log.i(TAG,"Notification removed"); 
  } 
  
  @Override 
  public void onNotificationPosted(StatusBarNotification sbn) { 
    Log.i(TAG, "Notification posted"); 
  } 
}

AndroidManifest.xml中聲明此服務(wù)類,并必須聲明BIND_NOTIFICATION_LISTENER_SERVICE許可和意圖過濾器

android.service.notification.NotificationListenerService,還有我們在系統(tǒng)設(shè)置中通知使用權(quán)列表中看到的label標簽:

<serviceandroid:name=".NotificationListener" 
     android:label="通知使用權(quán)測試程序" 
     android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"> 
  <intent-filter> 
    <actionandroid:name="android.service.notification.NotificationListenerService"/> 
  </intent-filter> 
 </service>

OK,就這么簡單就可以完成APP監(jiān)聽系統(tǒng)通知欄的功能了。接下來,我們來看看NotificationListenerService類還提供了一些重要的方法:

StatusBarNotification[] sbns = getActiveNotifications();         // 返回當前系統(tǒng)所有通知的數(shù)組 
cancelAllNotifications();                        // 刪除系統(tǒng)中所有可被清除的通知 
cancelNotification(String pkg, String tag, int id);           // 刪除具體某一個通知

還有上面我們提到的兩個重要的重寫方法:

onNotificationRemoved(StatusBarNotification sbn);            // 通知被移除時回調(diào) 
onNotificationPosted(StatusBarNotification sbn);             // 增加一條通知時回調(diào)

這兩個重要的回調(diào)方法它們的參數(shù)StatusBarNotification對象是當前觸發(fā)變化通知的詳細信息。來看下StatusBarNotification的使用:

sbn.getId();                               // 返回通知對應(yīng)的id 
sbn.getNotification();                          // 返回通知對象 
sbn.getPackageName();                          // 返回通知對應(yīng)的包名 
sbn.getPostTime();                            // 返回通知發(fā)起的時間 
sbn.getTag();                              // 返回通知的Tag,如果沒有設(shè)置返回null 
sbn.isClearable();                            // 返回該通知是否可被清楚,是否為FLAG_ONGOING_EVENT、FLAG_NO_CLEAR 
sbn.isOngoing();                             // 返回該通知是否在正在運行的,是否為FLAG_ONGOING_EVENT

其中,getNotification()返回的通知對象,還可以詳細看到通知的其它相關(guān)信息,如:

Notification notification = sbn.getNotification(); 
notification.contentView;                        // 通知的RemoteViews 
notification.contentIntent;                       // 通知的PendingIntent 
notification.actions;                          // 通知的行為數(shù)組 
// Android4.4后還擴展了可以獲取通知詳情信息 
if (Build.VERSION.SDK_INT >Build.VERSION_CODES.JELLY_BEAN_MR2) { 
     Bundle extras = notification.extras; 
     String notificationTitle = extras.getString(Notification.EXTRA_TITLE); 
     int notificationIcon = extras.getInt(Notification.EXTRA_SMALL_ICON); 
     Bitmap notificationLargeIcon = ((Bitmap)extras.getParcelable(Notification.EXTRA_LARGE_ICON)); 
     CharSequence notificationText = extras.getCharSequence(Notification.EXTRA_TEXT); 
     CharSequence notificationSubText = extras.getCharSequence(Notification.EXTRA_SUB_TEXT); 
}

跳轉(zhuǎn)系統(tǒng)設(shè)置里的通知使用權(quán)頁面

private boolean gotoNotificationAccessSetting(Contextcontext) { 
  try { 
    Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"); 
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    context.startActivity(intent); 
    return true; 
  } catch(ActivityNotFoundException e) { 
    try { 
      Intent intent = new Intent(); 
      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      ComponentName cn = new ComponentName("com.android.settings","com.android.settings.Settings$NotificationAccessSettingsActivity"); 
      intent.setComponent(cn); 
      intent.putExtra(":settings:show_fragment", "NotificationAccessSettings"); 
      context.startActivity(intent); 
      return true; 
    } catch(Exception ex) { 
      ex.printStackTrace(); 
    } 
    return false; 
  } 
}

判斷是否擁有通知使用權(quán)

private boolean notificationListenerEnable() { 
  boolean enable = false; 
  String packageName = getPackageName(); 
  String flat= Settings.Secure.getString(getContentResolver(),"enabled_notification_listeners"); 
  if (flat != null) { 
    enable= flat.contains(packageName); 
  } 
  return enable; 
}

感謝你能夠認真閱讀完這篇文章,希望小編分享的“Android中NotificationListenerService怎么用”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI