Android通知监听权限NotificationListener

Android通知监听权限NotificationListener

是否启用通知监听权限

通过 Settings.Secure.getString 获取启用了通知监听权限的列表,然后根据其中是否包含来判断

列表的字符串是这样的结构:应用包名称1/通知监听类全路径1:应用包名称2/通知监听类全路径2

例如包名为 shendi.notify,通知监听类在包下,名称为 MyNotifyService

复制代码
shendi.notify/shendi.notify.MyNotifyService

所以判断当前应用是否启用通知监听权限代码如下

java 复制代码
public static boolean isNotificationListenerEnabled(Context context) {
    String packageName = context.getPackageName();

    // 获取系统中所有启用了通知监听权限的包名
    String enabledListeners = Settings.Secure.getString(context.getContentResolver(), "enabled_notification_listeners");
    System.out.println(enabledListeners);
    if (enabledListeners != null && !TextUtils.isEmpty(enabledListeners)) {
        String[] packages = enabledListeners.split(":");
        for (String packageNameInList : packages) {
            if (packageNameInList.contains(packageName)) {
                return true;
            }
        }
    }

    return false;
}

跳转系统通知监听页

当没开通通知监听权限,一般需要跳转,代码如下

java 复制代码
Intent intent = new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS);
context.startActivity(intent);

END

相关推荐
阿巴斯甜15 小时前
Android 报错:Zip file '/Users/lyy/develop/repoAndroidLapp/l-app-android-ble/app/bu
android
Kapaseker16 小时前
实战 Compose 中的 IntrinsicSize
android·kotlin
xq952717 小时前
Andorid Google 登录接入文档
android
黄林晴18 小时前
告别 Modifier 地狱,Compose 样式系统要变天了
android·android jetpack
冬奇Lab1 天前
Android触摸事件分发、手势识别与输入优化实战
android·源码阅读
城东米粉儿1 天前
Android MediaPlayer 笔记
android
Jony_1 天前
Android 启动优化方案
android
阿巴斯甜1 天前
Android studio 报错:Cause: error=86, Bad CPU type in executable
android
张小潇1 天前
AOSP15 Input专题InputReader源码分析
android
_小马快跑_2 天前
Kotlin | 协程调度器选择:何时用CoroutineScope配置,何时用launch指定?
android