本文主要是介绍AndroidQ(10.0) SystemUI 增加Notification控制白名单,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
前言
定制系统一般都会要求状态栏左上角只显示固定的通知消息,避免预装其它app乱发通知消息
解决办法
找到 SystemUI 中控制消息现实的地方,将其拦截
frameworks\base\packages\SystemUI\src\com\android\systemui\statusbar\NotificationListener.java
private boolean isNeedAddNotification(String pkgName){java.util.List<String> pkgList = new java.util.ArrayList<String>();pkgList.clear();pkgList.add("android");return pkgList.contains(pkgName);}@Overridepublic void onNotificationPosted(final StatusBarNotification sbn,final RankingMap rankingMap) {if (DEBUG) Log.d(TAG, "onNotificationPosted: " + sbn);//cczheng add for notificationarea only show pstn and android icon if (sbn != null){String pkgName = sbn.getOpPkg();if (!isNeedAddNotification(pkgName)) {Log.d("NotificationListener","onNotificationPosted forbidden "+pkgName);return;}}//endif (sbn != null && !onPluginNotificationPosted(sbn, rankingMap)) {Dependency.get(Dependency.MAIN_HANDLER).post(() -> {processForRemoteInput(sbn.getNotification(), mContext);String key = sbn.getKey();boolean isUpdate =mEntryManager.getNotificationData().get(key) != null;// In case we don't allow child notifications, we ignore children of// notifications that have a summary, since` we're not going to show them// anyway. This is true also when the summary is canceled,// because children are automatically canceled by NoMan in that case.if (!ENABLE_CHILD_NOTIFICATIONS&& mGroupManager.isChildInGroupWithSummary(sbn)) {if (DEBUG) {Log.d(TAG, "Ignoring group child due to existing summary: " + sbn);}// Remove existing notification to avoid stale data.if (isUpdate) {mEntryManager.removeNotification(key, rankingMap, UNDEFINED_DISMISS_REASON);} else {mEntryManager.getNotificationData().updateRanking(rankingMap);}return;}if (isUpdate) {mEntryManager.updateNotification(sbn, rankingMap);} else {Log.d("NotificationListener", "addNotification: " + sbn);mEntryManager.addNotification(sbn, rankingMap);}});}}@Overridepublic void onNotificationRemoved(StatusBarNotification sbn, RankingMap rankingMap,int reason) {if (DEBUG) Log.d(TAG, "onNotificationRemoved: " + sbn + " reason: " + reason);//cczheng add for notificationarea only show pstn and android icon if (sbn != null){String pkgName = sbn.getOpPkg();if (!isNeedAddNotification(pkgName)) {Log.d("NotificationListener","onNotificationPosted forbidden "+pkgName);return;}}//endif (sbn != null && !onPluginNotificationRemoved(sbn, rankingMap)) {final String key = sbn.getKey();Dependency.get(Dependency.MAIN_HANDLER).post(() -> {mEntryManager.removeNotification(key, rankingMap, reason);});}}@Overridepublic void onNotificationRemoved(StatusBarNotification sbn, RankingMap rankingMap) {onNotificationRemoved(sbn, rankingMap, UNDEFINED_DISMISS_REASON);}
这里只保留 android 进程发送过来的通知,其余一律禁止显示,这里为了演示做的简陋,采用了list存储,
可根据需求增加 Provider 来增删改,来看下log
D/NotificationListener: onNotificationPosted: StatusBarNotification(pkg=android user=UserHandle{-1} id=26 tag=null key=-1|android|26|null|1000: Notification(channel=DEVELOPER pri=0 contentView=null vibrate=null sound=null tick defaults=0x0 flags=0x2 color=0xff607d8b vis=PUBLIC))
D/NotificationListener: addNotification: StatusBarNotification(pkg=android user=UserHandle{-1} id=26 tag=null key=-1|android|26|null|1000: Notification(channel=DEVELOPER pri=0 contentView=null vibrate=null sound=null tick defaults=0x0 flags=0x2 color=0xff607d8b vis=PUBLIC))
D/NotificationListener: onNotificationPosted: StatusBarNotification(pkg=com.android.dyboot user=UserHandle{0} id=4403 tag=null key=0|com.android.dyboot|4403|null|10069: Notification(channel=a3845e9cfc7d494593e892990b7c7bec pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x22 color=0x00000000 vis=PRIVATE))
D/NotificationListener: onNotificationPosted forbidden com.android.dyboot
这篇关于AndroidQ(10.0) SystemUI 增加Notification控制白名单的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!