Android监听系统通知
16lz
2021-01-23
Android监听系统通知
- 1. API简介
- 2. 系统监听功能实现
- 2.1 新建NotificationMonitor类继承自NotificationListenerService,这是监听系统消息的核心服务类
- 2.2 NotificationMonitor服务类需要在AndroidManifest.xml中注册
- 2.3 新建程序的主入口MainActivity类,并在onCreate方法中开启监听服务NotificationMonitor
- 2.4 由于系统消息监听权限需要手动设置,这里添加一个代码,打开设置页面,方便开启系统消息监听权限
- 2.5 监听到系统消息以后可以通过发广播的方式与MainActivity通信,将监听消息传递到前台页面,并显示在列表中
1. API简介
Android在4.3的版本中(即API 18)加入了NotificationListenerService,根据SDK的描述(AndroidDeveloper)可以知道,当系统收到新的通知或者通知被删除时,会触发NotificationListenerService的回调方法。同时在Android 4.4 中新增了Notification.extras 字段,也就是说可以继承NotificationListenerService获取系统通知具体信息。
然后一般会重写下面这三个方法:
onNotificationPosted(StatusBarNotification sbn) :当有新通知到来时会回调;onNotificationRemoved(StatusBarNotification sbn) :当有通知移除时会回调;onListenerConnected() :当 NotificationListenerService 是可用的并且和通知管理器连接成功时回调。
2. 系统监听功能实现
2.1 新建NotificationMonitor类继承自NotificationListenerService,这是监听系统消息的核心服务类
public class NotificationMonitor extends NotificationListenerService { @Override public void onNotificationPosted(StatusBarNotification sbn) { Bundle extras = sbn.getNotification().extras; // 获取接收消息APP的包名 String notificationPkg = sbn.getPackageName(); // 获取接收消息的抬头 String notificationTitle = extras.getString(Notification.EXTRA_TITLE); // 获取接收消息的内容 String notificationText = extras.getString(Notification.EXTRA_TEXT); Log.i("NotificationMonitor", "Notification posted " + notificationTitle + " & " + notificationText); } @Override public void onNotificationRemoved(StatusBarNotification sbn) { // TODO Auto-generated method stub Bundle extras = sbn.getNotification().extras; // 获取接收消息APP的包名 String notificationPkg = sbn.getPackageName(); // 获取接收消息的抬头 String notificationTitle = extras.getString(Notification.EXTRA_TITLE); // 获取接收消息的内容 String notificationText = extras.getString(Notification.EXTRA_TEXT); Log.i("NotificationMonitor", "Notification removed " + notificationTitle + " & " + notificationText); }}
2.2 NotificationMonitor服务类需要在AndroidManifest.xml中注册
2.3 新建程序的主入口MainActivity类,并在onCreate方法中开启监听服务NotificationMonitor
Intent intent = new Intent(this, NotificationMonitor.class); startService(intent);
同时需要在AndroidManifest.xml中注册MainActivity
2.4 由于系统消息监听权限需要手动设置,这里添加一个代码,打开设置页面,方便开启系统消息监听权限
public void openNotificationListenSettings() { try { Intent intent; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) { intent = new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS); } else { intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"); } startActivity(intent); } catch (Exception e) { e.printStackTrace(); }}
在Mainactivity的onCreate方法中调用openNotificationListenSettings()即可
2.5 监听到系统消息以后可以通过发广播的方式与MainActivity通信,将监听消息传递到前台页面,并显示在列表中
public class MonitorBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String title = intent.getStringExtra("notificationTitle"); String text = intent.getStringExtra("notificationText"); }}
同时需要在AndroidManifest.xml中注册MonitorBroadcastReceiver
最后在NotificationMonitor中接收到系统消息的地方发送广播就行了
public void onReceive(String notificationTitle, String notificationText) {Intent intent = new Intent("com.example.BROADCAST");intent.putExtra("notificationTitle", notificationTitle);intent.putExtra("notificationText",notificationText);sendBroadcast(intent);}
更多相关文章
- Android中onConfigurationChanged监视系统配置更新
- android 系统资源的加载和获取
- Mac系统下Android SDK更新以及ADT无法在线下载
- 【Android】Mac系统Android开发环境搭建
- Android下uptime获取系统启动和运行时间
- Android系统源码数据库(mmssms.db)(转)
- Android ALSA音频系统架构分析(1)----从Loopback了解Audio
- Android P 图形显示系统(四) Android VirtualDisplay解析
- Android消息通知