版權(quán)聲明:本文為博主原創(chuàng)文章,未經(jīng)博主允許不得轉(zhuǎn)載罕邀。
微博:厲圣杰
源碼:AndroidDemo/Notification
文中如有紕漏,歡迎大家留言指出。
此篇為 Android Notification 補充揩环,對 Notification 的基本操作可參考:Android Notification 詳解(一)——基本操作。最近在公司項目中幅虑,需要實現(xiàn)獲取通知欄相關(guān)通知數(shù)據(jù)的功能丰滑,對此進行了調(diào)研,此為 NotificationListenerService
的簡單總結(jié)倒庵。
實現(xiàn)此功能需要用到 NotificationListenerService
褒墨,該類主要用于獲取系統(tǒng)相關(guān)的通知信息,需要在 API 18(Android 4.3) 及更高版本中才能使用擎宝。如:新增或刪除通知郁妈、通知的數(shù)量、通知的相關(guān)信息等绍申。
NotificationListenerService
主要方法有:
- onNotificationPosted(StatusBarNotification sbn)
- onNotificationRemoved(StatusBarNotification sbn)
- cancelAllNotifications()
- cancelNotification(String pkg, String tag, int id)
- getActiveNotifications()
當我們繼承 NotificationListenerService
時噩咪,主要復(fù)寫 onNotificationPosted(StatusBarNotification sbn)
和 onNotificationRemoved(StatusBarNotification sbn)
锄奢,其余三個方法是 final 的,所以不能被復(fù)寫剧腻。
而 StatusBarNotification
則包含了 Notification 的相關(guān)信息拘央,如:通知的名稱、圖標书在、創(chuàng)建時間等灰伟。
實現(xiàn) NotificationListenerService
的使用可以分為以下幾步:
-
新建一個繼承自
NotificationListenerService
的子類,實現(xiàn)onNotificationPosted()
和onNotificationRemoved()
方法public class MyNotificationListenerService extends NotificationListenerService { private static final String TAG = MyNotificationListenerService.class.getSimpleName(); @Override public void onNotificationPosted(StatusBarNotification sbn) { super.onNotificationPosted(sbn); Log.d(TAG, "onNotificationPosted=" + sbn.toString()); } @Override public void onNotificationRemoved(StatusBarNotification sbn) { super.onNotificationRemoved(sbn); Log.d(TAG, "onNotificationRemoved=" + sbn.toString()); } }
-
在 AndroidManifest.xml 中申明相關(guān)的權(quán)限
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.littlejie.notification"> <uses-permission android:name="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <!-- something else --> <service android:name=".MyNotificationListenerService" android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"> <intent-filter> <action android:name="android.service.notification.NotificationListenerService" /> </intent-filter> </service> </application> </manifest>
-
僅僅實現(xiàn)上述兩個步驟還是不夠儒旬,這里栏账,我們還需要去開啟 Notification access。默認是在 Settings > Security > Notification access 中去開啟栈源,我們也可以通過 Intent 直接跳轉(zhuǎn)到對應(yīng)頁面挡爵。
public class NotificationListenerServiceActivity extends Activity { //此為 Settings 中的常量,不過是屬于隱藏字段 private static final String ENABLED_NOTIFICATION_LISTENERS = "enabled_notification_listeners"; private Button mBtnSetNotifyAccess; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_get_notification_bar); mBtnSetNotifyAccess = (Button) findViewById(R.id.btn_setting); mBtnSetNotifyAccess.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS); startActivity(intent); } }); findViewById(R.id.btn_start).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startService(new Intent(NotificationListenerServiceActivity.this, MyNotificationListenerService.class)); } }); } @Override protected void onResume() { super.onResume(); mBtnSetNotifyAccess.setText(String.format(getString(R.string.set_notification_access), isEnabled() ? "已開啟" : "未開啟")); } /** * 判斷 Notification access 是否開啟 * @return */ private boolean isEnabled() { String pkgName = getPackageName(); final String flat = Settings.Secure.getString(getContentResolver(), ENABLED_NOTIFICATION_LISTENERS); if (!TextUtils.isEmpty(flat)) { final String[] names = flat.split(":"); for (int i = 0; i < names.length; i++) { final ComponentName cn = ComponentName.unflattenFromString(names[i]); if (cn != null) { if (TextUtils.equals(pkgName, cn.getPackageName())) { return true; } } } } return false; } }
Crash
在 HTC Android 4.3 版本的手機上捕獲該 crash,原因不明甚垦,大意是應(yīng)該是系統(tǒng)沒有實現(xiàn) onNotificationPosted()
方法茶鹃,后在 Android 4.3 及 4.4 的模擬器上測試,均重現(xiàn)了該 crash艰亮,而 Android 5.0 及以上的系統(tǒng)都正常闭翩,推測是系統(tǒng)原因,因此迄埃,Android 5.0 以下使用 NotificationListenerService
需慎重疗韵。
12-07 19:35:55.350 1210-1236/com.littlejie.notification E/JavaBinder: *** Uncaught remote exception! (Exceptions are not yet supported across processes.)
java.lang.AbstractMethodError: abstract method not implemented
at android.service.notification.NotificationListenerService.onNotificationPosted(NotificationListenerService.java)
at com.littlejie.notification.MyNotificationListenerService.onNotificationPosted(MyNotificationListenerService.java:17)
at android.service.notification.NotificationListenerService$INotificationListenerWrapper.onNotificationPosted(NotificationListenerService.java:167)
at android.service.notification.INotificationListener$Stub.onTransact(INotificationListener.java:56)
at android.os.Binder.execTransact(Binder.java:388)
at dalvik.system.NativeStart.run(Native Method)
12-07 19:35:55.350 1210-1236/com.littlejie.notification W/dalvikvm: threadid=9: thread exiting with uncaught exception (group=0x415f6970)
12-07 19:35:55.360 1210-1236/com.littlejie.notification E/AndroidRuntime: FATAL EXCEPTION: Binder_1
java.lang.AbstractMethodError: abstract method not implemented
at android.service.notification.NotificationListenerService.onNotificationPosted(NotificationListenerService.java)
at com.littlejie.notification.MyNotificationListenerService.onNotificationPosted(MyNotificationListenerService.java:17)
at android.service.notification.NotificationListenerService$INotificationListenerWrapper.onNotificationPosted(NotificationListenerService.java:167)
at android.service.notification.INotificationListener$Stub.onTransact(INotificationListener.java:56)
at android.os.Binder.execTransact(Binder.java:388)
at dalvik.system.NativeStart.run(Native Method)
補充
后續(xù)需要實現(xiàn)當應(yīng)用被殺,但是 App 仍能接受到 Notification 通知侄非,即繼承自 NotificationListenerService
的 Service 仍然需要存活蕉汪,本以為這是一個浩大工程,需要 Service 之間相互喚起逞怨,結(jié)果測試的時候發(fā)現(xiàn) NotificationListenerService 不會被殺死者疤,實在是很不解,網(wǎng)上搜索之后才發(fā)現(xiàn)原因:繼承自 NotificationListenerService
的 Service 被殺死后骇钦,仍會被系統(tǒng)以各種方式重新調(diào)起宛渐,這不失為一個讓 App 永生不滅的方法竞漾,除了要讓用戶手動授權(quán)獲取 Notification access 權(quán)限眯搭。
具體分析詳情見:Android之使用NotificationListenerService使得自己的應(yīng)用不被殺及其源碼分析