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

Android9-W517-使用NotificationListenerService监听通知

目录

一、前言

二、前提

三、方案

方案一

方案二

方案三

方案四

方案五

方案六

方案七

四、关于NotificationListenerService类头注释

五、结论


一、前言

        NotificationListenerService可以让应用监听所有通知,但是无法获得监听通知的权限,如下六种方案暂时均未实现,最终验证方案七+方案二可行

二、前提

  1. 应用:客制化桌面(com.**.launcher);

  2. 应用内置在 /system/priv-app/ 下;

  3. 应用没有设置:android:sharedUserId="android.uid.system"。

三、方案

方案一

        通过Action跳转《系统设置》应用,手动打开通知监听权限:android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS——结果如图:显示在此设备上不能获得此特性——暂不可行;

    if (!AppUtils.isNotificationListenersEnabled(context, context.packageName)) {AppUtils.gotoNotificationAccessSetting(context)}fun isNotificationListenersEnabled(context: Context, packageName: String): Boolean =NotificationManagerCompat.getEnabledListenerPackages(context).contains(packageName)fun gotoNotificationAccessSetting(context: Context): Boolean {return try {val intent = Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS")intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)context.startActivity(intent)true} catch (e: ActivityNotFoundException) {// 普通情况下找不到的时候需要再特殊处理找一次try {val intent = Intent()intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)val cn = ComponentName("com.android.settings","com.android.settings.Settings\$NotificationAccessSettingsActivity")intent.component = cnintent.putExtra(":settings:show_fragment", "NotificationAccessSettings")context.startActivity(intent)return true} catch (e1: java.lang.Exception) {e1.printStackTrace()}//            Toast.makeText(this, "对不起,您的手机暂不支持", Toast.LENGTH_SHORT).show()e.printStackTrace()false}}

方案二

        在源码frameworks/base/core/res/res/values/config.xml路径下,修改config_defaultListenerAccessPackages属性的值为应用包名com.***.launcher——在之前的Android11、Android12上此种方案可行,但在W517的Android9上暂不可行;    

  

方案三

        接着方案一,把设置应用的低内存检查去掉,但在通知访问权限弹窗中,点击Allow后系统同样不生效,甚至申请通知访问权限的应用【Launcher2D】会被杀死后重启——暂不可行;

方案四

        在系统初次初始化时就将通知权限加入secure 数据库中,但是依旧未生效——暂不可行;

  

方案五

        在应用启动时,去重新关闭打开一次监听服务,但是它依旧未正常工作——暂不可行;

    private fun toggleNotificationListenerService() {Log.e("TAG", "toggleNotificationListenerService: ")val pm: PackageManager = context.packageManagerpm.setComponentEnabledSetting(ComponentName(context, MNotificationListenerService::class.java), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP)pm.setComponentEnabledSetting(ComponentName(context, MNotificationListenerService::class.java), PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP)}

方案六

        如果是某些厂商没有采用google那一套通知逻辑,修改了系统源码,比如:Binder接口被修改,在通知的时候如果采用google那一套,则极有获取不到通知,但即使无论怎么改,调用修改后源码那一套super(arg0)总是没错的;可以试着在NotificationTask类中onBind方法中返回return super.onBind(arg0)——暂不可行

class MNotificationListenerService : NotificationListenerService() {private val TAG = MNotificationListenerService::class.java.simpleNameoverride fun onBind(intent: Intent?): IBinder? {return super.onBind(intent)}}

方案七

        接着方案一,修改设备为非低内存设备,即将ActivityManager的isLowRamDeviceStatic方法直接返回false,如下,编译系统后验证OK...【开心】

@SystemService(Context.ACTIVITY_SERVICE)
public class ActivityManager { public static boolean isLowRamDeviceStatic() {
//        return RoSystemProperties.CONFIG_LOW_RAM ||
//                (Build.IS_DEBUGGABLE && DEVELOPMENT_FORCE_LOW_RAM);return false;}}

        原因如下: 参考第四节NotificationListenerService类头注释Android10及更低版本的低RAM设备上,NotificationListenerService是无法获取通知访问权限的。

        解决方案:关掉低内存设备的判断逻辑,直接返回false。

四、关于NotificationListenerService类头注释

       

1、 Android 11/12的NotificationListenerService类头注释中有如下内容:

        Notification listeners cannot get notification access or be bound by the system on low-RAM devices running Android Q (and below).

        翻译过来就是说:在运行 Android Q(及更低版本)的低 RAM 设备上,通知侦听器无法获取通知访问权限或被系统绑定。

       

2、 Android 9的NotificationListenerService类头注释中有如下内容:

        Notification listeners cannot get notification access or be bound by the system on low-RAM devices.

        翻译过来就是说:在低 RAM 设备上,通知侦听器无法获取通知访问权限或被系统绑定。

五、结论

        使用方案七+方案二即可。


http://www.lryc.cn/news/312012.html

相关文章:

  • git的“You can‘t push commits with committe“解决方法
  • CAN总线的拓扑类型和CAN收发器(原理讲解)
  • 如何实现WordPress后台显示文章、分类目录、标签等的ID?
  • 【GB28181】SIP协议实践之Windows下VS2019编译eXosip、osip,测试(附工程源码,一键打开编译)
  • GPT提示语格式——个人自用
  • MCU最小系统电路设计(以STM32F103C8T6为例)
  • [JavaWeb学习日记]JSP+Cookie+Filter与登录+CRUD案例
  • Ruby网络爬虫教程:从入门到精通下载图片
  • 各中间件性能、优缺点对比
  • 修改表中某个字段等于另一个字段减去 2 小时的 SQL
  • Jetpack Compose: Hello Android
  • 蓝桥每日一题 (差分)3月3号
  • Mybatis和Spring Data Jpa的优缺点比较(八股文)
  • LeetCode买卖股票的最佳时机
  • Codeforces Round 932 (Div. 2)----->A. Entertainment in MAC
  • 【JavaScript】 短路运算的妙用 ||
  • 密码学之椭圆曲线
  • overleaf latex 笔记
  • 第十五届蓝桥杯青少组STEMA测评SPIKE初级真题试卷 2024年1月
  • 10个常见的Java面试问题及其答案
  • Vue跳转页面传递参数
  • 【已解决】conda环境下ROS2 colcon build编译选择特定python解释器
  • QT C++实践| 连接数据库的登录界面实现| 附源码
  • html样式排版
  • Java:性能优化细节31-45
  • YOLOv9独家原创改进|增加SPD-Conv无卷积步长或池化:用于低分辨率图像和小物体的新 CNN 模块
  • Android Gradle开发与应用 (四) : Gradle构建与生命周期
  • [MRCTF2020]Transform1
  • JavaWeb HTTP 请求头、请求体、响应头、响应体、响应状态码
  • 穿越数字防线:SSH协议的全景解析与未来展望