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

【Android源码】屏蔽系统通知出现在系统栏中

环境

aosp: android-14.0.0_r1
真机:Pixel5

目标

我想把usb连接的两个系统通知屏蔽掉

  1. “正在通过 USB 为此设备充电”
  2. “已连接到 USB 调试”

步骤

  1. 首先找到这两句内容出现的位置在
    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>
  1. 然后找到代码实现的位置
    分别是:
    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);
}

很简单吧,最后测试通过!


http://www.mrgr.cn/news/31238.html

相关文章:

  • Netty源码解析-请求处理与多路复用
  • 甲基化组学全流程分析(适用于27k的甲基化数据)
  • 基于SpringBoot+Vue的校园失物招领系统
  • STL 源码剖析 | 第1章:概论
  • git merge如何忽略部分路径
  • 【数据结构与算法 | 灵神题单 | 二叉搜索树篇】力扣99, 1305, 230, 897
  • 模板字符串中定义方法并传参
  • SpringBoot 项目如何使用 pageHelper 做分页处理 (含两种依赖方式)
  • 如何理解变量提升和函数提升
  • Lichee NanoKVM基本使用环境
  • 【AI写作】介绍 Docker 的基本概念和优势,以及在应用程序开发中的实际应用
  • OpenAI o1解决了Quiet-STaR的挑战吗?(下)
  • Java 并发编程 —— AQS 抽象队列同步器
  • 信奥初赛解析:1-3-计算机软件系统
  • ARM/Linux嵌入式面经(三七):CVTE
  • LTE和NR空口技术区别
  • Redis基本命令详解
  • 二、pandas入门
  • arkts基础知识
  • 实例代码逐行解读