山东网站建设软件,网页设计与网站建设书,做易拉宝的素材网站,经营虚拟网站策划书目录
一、前言
二、前提
三、方案
方案一
方案二
方案三
方案四
方案五
方案六
方案七
四、关于NotificationListenerService类头注释
五、结论 一、前言 NotificationListenerService可以让应用监听所有通知#xff0c;但是无法获得监听通知的权限#xff0c;如…目录
一、前言
二、前提
三、方案
方案一
方案二
方案三
方案四
方案五
方案六
方案七
四、关于NotificationListenerService类头注释
五、结论 一、前言 NotificationListenerService可以让应用监听所有通知但是无法获得监听通知的权限如下六种方案暂时均未实现最终验证方案七方案二可行。 二、前提 应用客制化桌面com.**.launcher 应用内置在 /system/priv-app/ 下 应用没有设置android:sharedUserIdandroid.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 设备上通知侦听器无法获取通知访问权限或被系统绑定。 五、结论 使用方案七方案二即可。