有些app(例如qq)爲了讓app的通知欄看起來輕鬆。對自己的通知欄信息做了合併,這樣我們想要監聽app收到的所有通知必須從系統層面來想辦法了,NotificationListenerService API 18;
1 services
創建一個實現NotificationListenerService的服務,
如果只是監聽通知的顯示和取消只需要在服務裏重寫通知顯示監聽onNotificationPosted和通知移除onNotificationRemoved即可。
在Manifest註冊
<service
android:name=".NLService"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
判斷是否授權:
private boolean isNotificationServiceEnable() {
return NotificationManagerCompat.getEnabledListenerPackages(this).contains(getPackageName());
}
跳轉授權頁面:
startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));