当前位置: 首页 > news >正文

Android 12.0 应用中监听系统收到的通知

1. 需求

在系统内置应用中或者在第三方应用中,获取Android系统收到的通知的内容.

2. NotificationListenerService 接口

Android 系统预留了专门的API, 即 NotificationListenerService 接口,它的源码路径为:

源码路径 : /frameworks/base/core/java/android/service/notification/NotificationListenerService.javapublic abstract class NotificationListenerService extends Service {......

3. 实现 NotificationListenerService

NotificationListenerService 是抽象类,通过在 Service 中实现该抽象类,并实现需要的接口,代码如下:

public class MyNotificationListenerService extends NotificationListenerService {//当系统收到新的通知时,会触发该接口@Overridepublic void onNotificationPosted(StatusBarNotification sbn) {addAlienNotificationInfo(sbn); //获取通知的内容}//当系统移除通知时,即在通知列表中清除通知时,或者卸载应用时,该应用包名下的通知都会被清除,也同样会会触发该接口@Overridepublic void onNotificationRemoved(StatusBarNotification sbn) {super.onNotificationRemoved(sbn);}
}

上面两个接口,第一个是监听系统新通知,第二个是监听系统通知清除,如果看过Android Systemui 中有关通知的内容,就会发现,System UI 对通知的显示和通知的清除,同样也是继承该API.

下面,分析当系统收到新通知时,如何解析出通知里的内容.

4. 获取通知内容

   private void addAlienNotificationInfo(StatusBarNotification sbn) {String packageName = sbn.getPackageName();//获取发送通知的包名Notification notification = sbn.getNotification(); //获得一个Notification对象Bundle extras = notification.extras;RemoteViews contentView = notification.contentView;//ApplicationInfo appInfo = extras.getParcelable(NotificationCompat.EXTRA_BUILDER_APPLICATION_INFO);//如果是系统内置应用,可以获取到ApplicationInfo ,后面会有解释//String appName = appInfo.loadLabel(mPm).toString(); //如果是系统内置的应用,可以通过ApplicationInfo对象获取通知发送这条通知的应用名String category = notification.category;String channelId = notification.getChannelId();//String className = getNotificationClassName(notification);//如果是系统内置应用,可以获取到通知中设置的Intent,后面会有解释int color = notification.color;//通知setColor()设置的颜色boolean defaultVibrate = (notification.vibrate == null) ? true : false;String notificationVibrationString = Arrays.toString(notification.vibrate);int importance = notification.priority; //通知的重要性String key = sbn.getKey();//通知的Key,删除通知时,需要通过Key来确定删除哪条通知Icon myLargeIconToIcon = extras.getParcelable(Notification.EXTRA_LARGE_ICON);//获取通知设置的大图片,即setLargeIcon() ,int ledColor = notification.ledARGB;//通知led灯颜色String sound = (notification.sound != null) ? notification.sound.toString() : null;int progress = extras.getInt(Notification.EXTRA_PROGRESS);//当前进度值int progressMax = extras.getInt(Notification.EXTRA_PROGRESS_MAX);//设定的最大进度值boolean progressIndeterminate = extras.getBoolean(Notification.EXTRA_PROGRESS_INDETERMINATE);//是否在通知中显示进度值int flagsToCheck = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;boolean resident = (notification.flags & flagsToCheck) != 0;//是否是常驻通知(前台&onging),类似于音乐应用,String smallIcon = notification.getSmallIcon().toString();//通知设置的smallIcon()String title = (String) extras.get(Notification.EXTRA_TITLE);//通知标题String subText = (String) extras.get(Notification.EXTRA_SUB_TEXT);//通知附加内容String text = (String) extras.get(Notification.EXTRA_TEXT);//通知内容boolean userRemovable = (notification.flags & Notification.FLAG_AUTO_CANCEL) != 0; //是否可移除通知,即 setAutoCancel(boolean autoCancel) 中设定的值long when = notification.when;//通知的时间//通过下面的方法也可以查看通知中的设置的全部参数for(String mykey : notification.extras.keySet()){String ex = " " + mykey +" => " + notification.extras.get(mykey)+ ";";Log.d(TAG,"ex="+ex);}
}

上面在获取通知的ApplicationInfo时,使用了 Notification.EXTRA_BUILDER_APPLICATION_INFO,该值不对系统外开放,如下源码所示:

源码路径:/frameworks/base/core/java/android/app/Notification.java/*** @hide*/public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";

上面还涉及到了getNotificationClassName(notification) 获取这条通知中通过PendingIntent中设置的 Intent ,方法中用到了不对外使用的方法,代码如下:

 private String getNotificationClassName(Notification notification) {if (notification.contentIntent != null) {Intent intent = notification.contentIntent.getIntent();if (intent != null && intent.getComponent() != null) {return intent.getComponent().getClassName();}}return null;}

其中的 notification.contentIntent 是获取 通知中的 PendingIntent 对象,源码如下:

源码路径:/frameworks/base/core/java/android/app/Notification.java /*** 单击展开的状态条目时要执行的意图。*/public PendingIntent contentIntent;

接着再通过调用 PendingIntent 对象中的 getIntent() 来获取通知中设定的 Intent, 源码如下:

源码路径: /frameworks/base/core/java/android/app/PendingIntent.java/*** @hide (该方法不对外公开)* 返回 PendingIntent 中的 Intent .*/@UnsupportedAppUsagepublic Intent getIntent() {try {return ActivityManager.getService().getIntentForIntentSender(mTarget);} catch (RemoteException e) {throw e.rethrowFromSystemServer();}}

 由于源码中有@hide,表明该方法不对第三方应用开放,所以如果是系统内置的应用,则可以使用.

5. 通知的清除 

当通知被清除时,会调用 onNotificationRemoved(StatusBarNotification sbn) 方法,其中的参数 sbn 代表被删除的通知.可以在该方法里做一些删除后的工作.

6 . NotificationListenerService 中其他有用的方法

(1) 获取有效的通知 : getActiveNotifications() 

源码路径: /frameworks/base/core/java/android/service/notification/NotificationListenerService.java/*** 请求未完成通知的列表(即那些对当前用户)。** @return 活动通知数组,按自然顺序排序。*/public StatusBarNotification[] getActiveNotifications() {StatusBarNotification[] activeNotifications = getActiveNotifications(null, TRIM_FULL);return activeNotifications != null ? activeNotifications : new StatusBarNotification[0];}

(2) 删除指定单条通知 : cancelNotification(String key)   

源码路径: /frameworks/base/core/java/android/service/notification/NotificationListenerService.java /*** 删除指定的一条通知**/public final void cancelNotification(String key) {if (!isBound()) return;//是否绑定了NotificationListenerService服务try {getNotificationInterface().cancelNotificationsFromListener(mWrapper,new String[] { key });} catch (android.os.RemoteException ex) {Log.v(TAG, "Unable to contact notification manager", ex);}}

 (3) 删除指定的多条通知: cancelNotifications(String[] keys)

源码路径: /frameworks/base/core/java/android/service/notification/NotificationListenerService.java /*** 删除 数组 keys 中指定key的通知*/public final void cancelNotifications(String[] keys) {if (!isBound()) return;try {getNotificationInterface().cancelNotificationsFromListener(mWrapper, keys);} catch (android.os.RemoteException ex) {Log.v(TAG, "Unable to contact notification manager", ex);}}

(4) 清除所有通知,对应于通知列表下的 清除所有通知的按钮功能: cancelAllNotifications()

  /*** 通知通知管理器清除所有通知* 类似于Android状态栏和通知面板调用 UI 的“全部关闭”功能* 收到通知后,通知管理器实际上会删除所有活动通知* 并且收到多个 {@link #onNotificationRemoved(StatusBarNotification)} 回调。*/public final void cancelAllNotifications() {cancelNotifications(null /*all*/);}

至此,关于监听系统通知介绍完毕,谢谢观看!

相关文章:

  • 代码随想录算法训练营第36天 | 435.无重叠区间 + 763.划分字母区间 + 56.合并区间
  • 投标书撰写注意事项
  • HiveSQL题——数据炸裂和数据合并
  • 海外短剧系统国际短剧源码h5多语言版app挂载tiktok油管ins
  • Linux系统-学习
  • LaTeX 文本对齐:ragged2e 宏包
  • 【大厂AI课学习笔记】1.3 人工智能产业发展(4)——泛在的人工智能
  • 提升小波变换的程序演示
  • 什么是系统工程(字幕)13
  • Shell - 学习笔记 - 2.6 - Shell $*和$@之间的区别
  • 延伸与应用(三)婚姻与经济、运动、宗教、科技与经济
  • Windows11通过Hyper-V创建VM,然后通过vscode连接vm进行开发
  • 我的数据结构c(给自己用的)
  • 无需 Root 卸载手机预装软件,精简过的老年机又行了
  • asp.net 404页面配置、 asp.net MVC 配置404页面、iis 配置404页面,指定404错误页面,设置404错误页面
  • 【跃迁之路】【519天】程序员高效学习方法论探索系列(实验阶段276-2018.07.09)...
  • 2018以太坊智能合约编程语言solidity的最佳IDEs
  • Angular Elements 及其运作原理
  • gf框架之分页模块(五) - 自定义分页
  • Git学习与使用心得(1)—— 初始化
  • Java 最常见的 200+ 面试题:面试必备
  • 闭包,sync使用细节
  • 给第三方使用接口的 URL 签名实现
  • 基于Dubbo+ZooKeeper的分布式服务的实现
  • 基于web的全景—— Pannellum小试
  • 前端技术周刊 2019-01-14:客户端存储
  • 微信端页面使用-webkit-box和绝对定位时,元素上移的问题
  • 扩展资源服务器解决oauth2 性能瓶颈
  • ​configparser --- 配置文件解析器​
  • $.ajax,axios,fetch三种ajax请求的区别
  • $Django python中使用redis, django中使用(封装了),redis开启事务(管道)
  • (1)(1.13) SiK无线电高级配置(六)
  • (20)目标检测算法之YOLOv5计算预选框、详解anchor计算
  • (27)4.8 习题课
  • (33)STM32——485实验笔记
  • (5)STL算法之复制
  • (M)unity2D敌人的创建、人物属性设置,遇敌掉血
  • (ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY)讲解
  • (ZT) 理解系统底层的概念是多么重要(by趋势科技邹飞)
  • (剑指Offer)面试题34:丑数
  • (三)mysql_MYSQL(三)
  • (数位dp) 算法竞赛入门到进阶 书本题集
  • (四)docker:为mysql和java jar运行环境创建同一网络,容器互联
  • (已解决)报错:Could not load the Qt platform plugin “xcb“
  • (转)chrome浏览器收藏夹(书签)的导出与导入
  • .apk文件,IIS不支持下载解决
  • .NET Framework杂记
  • .NET 的静态构造函数是否线程安全?答案是肯定的!
  • .NET序列化 serializable,反序列化
  • .sh文件怎么运行_创建优化的Go镜像文件以及踩过的坑
  • /etc/shadow字段详解
  • /usr/bin/perl:bad interpreter:No such file or directory 的解决办法
  • @TableLogic注解说明,以及对增删改查的影响
  • [1] 平面(Plane)图形的生成算法
  • [52PJ] Java面向对象笔记(转自52 1510988116)