使用NotificationListenerService监听通知栏消息
2017-01-13 14:07
495 查看
NotificationListenerService是通过系统调起的服务,在应用发起通知时,系统会将通知的应用,动作和信息回调给NotificationListenerService。但使用之前需要引导用户进行授权。使用NotificationListenerService一般需要下面三个步骤。
用户授权后就可以对通知栏的所有信息进行监听了。
注册服务
首先需要在AndroidManifest.xml对service进行注册。<service android:name=".NotificationCollectorService" android:label="@string/app_name" android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"> <intent-filter> <action android:name="android.service.notification.NotificationListenerService" /> </intent-filter> </service>
继承实现NotificationListenerService
自己实现一个继承NotificationListenerService的service,在onNotificationPosted中完成自己需要的操作。public class NotificationCollectorService extends NotificationListenerService { @Override public void onNotificationPosted(StatusBarNotification sbn) { Log.i("xiaolong", "open" + "-----" + sbn.getPackageName()); Log.i("xiaolong", "open" + "------" + sbn.getNotification().tickerText); Log.i("xiaolong", "open" + "-----" + sbn.getNotification().extras.get("android.title")); Log.i("xiaolong", "open" + "-----" + sbn.getNotification().extras.get("android.text")); } @Override public void onNotificationRemoved(StatusBarNotification sbn) { Log.i("xiaolong", "remove" + "-----" + sbn.getPackageName()); } }
引导用户进行授权
由于此服务需要用户手动进行授权,所以使用前需要对用户进行引导设置。public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); String string = Settings.Secure.getString(getContentResolver(), "enabled_notification_listeners"); if (!string.contains(NotificationCollectorService.class.getName())) { startActivity(new Intent( "android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS")); } } }
用户授权后就可以对通知栏的所有信息进行监听了。
相关文章推荐
- android使用NotificationListenerService监听通知栏消息
- 【笔记】Android NotificationListenerService监听短信、来电、微信、QQ等通知消息
- Android 通过系统使用NotificationListenerService 监听各种Notification的使用方法
- Android 通过系统使用NotificationListenerService 监听各种Notification的用法
- Android使用NotificationListenerService获取通知相关信息
- Android 通过系统使用NotificationListenerService 监听各种Notification的使用方法
- NotificationListenerService 监听应用程序消息
- 通知使用权(NotificationListenerService)的使用
- Android 通知使用权(NotificationListenerService)的使用
- Android通知栏监听之NotificationListenerService
- Android 通过系统使用NotificationListenerService 监听各种Notification的使用方法
- Notification使用详解之三:通过服务更新进度通知&在Activity中监听服务进度
- Notification使用详解之三:通过服务更新进度通知&在Activity中监听服务进度
- Notification使用详解之三:通过服务更新进度通知&在Activity中监听服务进度
- 使用Notification发送消息通知
- Android之使用NotificationListenerService使得自己的应用不被杀及其源码分析
- 通知栏发送消息Notification(可以使用自定义的布局)
- Notification使用详解之三:通过服务更新进度通知&在Activity中监听服务进度
- Notification使用 通过服务更新进度通知&在Activity中监听服务进度