Android(安卓)屏蔽系统通知的几种方法
16lz
2021-01-26
一、屏蔽SystemUI通知
SystemUI是系统级的apk,其路径为frameworks\base\packages\SystemUI,其主要功能是显示电池信号、通知面板、系统信息、以及第三方应用通知等等。这里列举屏蔽第三方应用等系统底部通知的方法:
路径:frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/BaseStatusBar.java
protected NotificationData.Entry createNotificationViews(StatusBarNotification sbn) { if (DEBUG) { Log.d(TAG, "createNotificationViews(notification=" + sbn); } final StatusBarIconView iconView = createIcon(sbn); if (条件){ return null; } NotificationData.Entry entry = new NotificationData.Entry(sbn, iconView); if (!inflateViews(entry, mStackScroller)) { handleNotificationError(sbn, "Couldn't expand RemoteViews for: " + sbn); return null; } return entry; }
从上述代码可得知,当我们不想让通知出现,只需要在createNotificationViews返回null即可。
二、屏蔽特定通知的方法
众所周知Notification就是通知类,发出的通知必然与它有关
其所在的路径为:frameworks/base/core/java/android/app/Notification.java
想要屏蔽特定的通知,可以选择获取到特定通知的内容,以此来做一个判断(假设要屏蔽aiqiyi的通知)
public static boolean isAiqiyiExist = false; public Builder setContentText(CharSequence text) { mText = safeCharSequence(text); if(mText != null && mText.equals("")){ int result = mText.toString.indexOf("aiqiyi"); if(result != -1){ isAiqiyiExist = true; } } return this; }
上述代码定义isAiqiyiExist标志位,当捕捉到此类通知时,会将这个标志位为true。
Notification的显示是在NotificationManager,其路径为:frameworks/base/core/java/android/app/NotificationManager.java
当通知要显示时会调用其中的notify方法
public void notify(int id, Notification notification) { notify(null, id, notification); }
这就是其中的notify方法,所以我们可以加判断来让它有条件的隐藏
public void notify(int id, Notification notification) { if(!notification.isAiqiyiExist){ notify(null, id, notification); } }
目前就只发现这两种,未完待续。。。。
更多相关文章
- Android(安卓)实现微信,QQ文件在其他应用打开并获取到路径
- Android(安卓)选择文件并获取路径
- Android(安卓)NDK之JNI陷阱
- /mnt/sdcard与Environment.getExternalStorageDirectory()的使用
- Error:Execution failed for task ':demo:packageRelease'. > Un
- class文件生成dex文件的步骤(命令行操作)
- Mac删除JDK
- Android(安卓)Studio Checkout GitHub Error:cannot run program
- Android(安卓)开发中屏蔽物理键盘