You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何通过ADB命令为监听通知的应用开启Notification Access权限

通过ADB开启应用的Notification Access权限

嘿,我之前刚好解决过同样的问题,给你分享下具体的操作步骤,亲测在模拟器上有效!

Android的通知访问权限存在系统设置的secure数据库里,我们只需要通过ADB命令修改对应的字段,就能直接开启权限,不用手动点设置界面。

步骤1:确定你的应用的通知监听器组件名

这个组件名是你在应用的AndroidManifest.xml里注册通知监听器时的完整路径,格式为[应用包名]/[监听器类的完整路径]。比如:

  • 如果你的应用包名是com.example.notifylistener
  • 监听器类是com.example.notifylistener.MyNotificationListener
    那组件名就是com.example.notifylistener/com.example.notifylistener.MyNotificationListener

要是不确定组件名,你可以直接查看应用的Manifest文件,或者用ADB命令先查看已开启的监听列表(如果之前没开启过可能返回空):

adb shell dumpsys notification | grep "enabled_notification_listeners"

步骤2:用ADB修改系统设置

首先,你可以先查看当前已经开启通知监听的应用列表(可选,用来确认是否需要追加你的应用):

adb shell settings get secure enabled_notification_listeners

然后分两种情况执行设置命令:

  • 如果当前没有其他应用开启通知监听,直接设置你的组件名:
adb shell settings put secure enabled_notification_listeners "你的组件完整路径"
  • 如果已经有其他应用开启,用冒号:分隔追加你的组件:
adb shell settings put secure enabled_notification_listeners "已有组件路径:你的组件完整路径"

步骤3:让设置生效(可选)

有时候修改后需要重启系统UI或者模拟器才能让权限生效,你可以执行这条命令重启系统UI:

adb shell am restart com.android.systemui

或者直接重启模拟器也能达到效果。

举个实际例子

假设我的应用包名是com.myapp.notifylistener,监听器类是NotificationListenerImpl,对应的组件名是com.myapp.notifylistener/com.myapp.notifylistener.NotificationListenerImpl,那完整命令就是:

adb shell settings put secure enabled_notification_listeners "com.myapp.notifylistener/com.myapp.notifylistener.NotificationListenerImpl"

执行完后,你可以去模拟器的设置→安全→Notification Access里确认,应该已经显示你的应用处于开启状态了!

内容的提问来源于stack exchange,提问作者hamed

火山引擎 最新活动