【Android源码】屏蔽系统通知出现在系统栏中
环境
aosp: android-14.0.0_r1
真机:Pixel5
目标
我想把usb连接的两个系统通知屏蔽掉
- “正在通过 USB 为此设备充电”
- “已连接到 USB 调试”
步骤
- 首先找到这两句内容出现的位置在
frameworks/base/core/res/res/values-zh-rCN/strings.xml
<string name="usb_charging_notification_title" msgid="1674124518282666955">"正在通过 USB 为此设备充电"</string><string name="adb_active_notification_title" msgid="408390247354560331">"已连接到 USB 调试"</string>
<string name="adb_active_notification_message" msgid="5617264033476778211">"点按即可关闭 USB 调试"</string>
- 然后找到代码实现的位置
分别是:
frameworks/base/core/java/android/debug/AdbNotifications.java
frameworks/base/services/usb/java/com/android/server/usb/UsbDeviceManager.java
Notification.Builder builder = new Notification.Builder(mContext, channel).setSmallIcon(com.android.internal.R.drawable.stat_sys_adb).setWhen(0).setOngoing(true).setTicker(title).setDefaults(0) // please be quiet.setColor(mContext.getColor(com.android.internal.R.color.system_notification_accent_color)).setContentTitle(title).setContentText(message).setContentIntent(pi).setVisibility(Notification.VISIBILITY_PUBLIC);if (titleRes== com.android.internal.R.string.usb_unsupported_audio_accessory_title) {builder.setStyle(new Notification.BigTextStyle().bigText(message));}Notification notification = builder.build();mNotificationManager.notifyAsUser(null, id, notification,UserHandle.ALL);Slog.d(TAG, "push notification:" + title);Log.w(TAG, "push notification:title=" + title + ";message=" + message);mUsbNotificationId = id;if (!mAdbNotificationShown) {Notification notification = AdbNotifications.createNotification(mContext,AdbTransportType.USB);mAdbNotificationShown = true;Log.w(TAG, "updateAdbNotification:id=" + id);mNotificationManager.notifyAsUser(null, id, notification, UserHandle.ALL);
}
主要是mNotificationManager.notifyAsUser()这个方法,它会把内容都通知出去。所以处理它就行,如果不想要所有的系统通知,屏蔽掉它就行。但我只是不想要两个内容。
修改如下:
Notification notification = builder.build();
if(message != null && !message.equals(r.getText(com.android.internal.R.string.usb_notification_message))) {mNotificationManager.notifyAsUser(null, id, notification,UserHandle.ALL);
}if (!mAdbNotificationShown) {Notification notification = AdbNotifications.createNotification(mContext, AdbTransportType.USB);mAdbNotificationShown = true;//mNotificationManager.notifyAsUser(null, id, notification, UserHandle.ALL);
}
很简单吧,最后测试通过!