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

相关推荐
心平气和量大福大1 小时前
android-控件-搜索框SearchView
android·gitee
2601_962177131 小时前
【MySQL篇】聚合查询,联合查询
android·java·mysql
壮哥_icon2 小时前
【Android】批量 VectorDrawable 动态分别修改 fillColor 和 strokeColor 的踩坑与终极解决方案
android
齐天qaq3 小时前
Android 系统 APK 分区与权限
android
心平气和量大福大3 小时前
android-控件-多选框CheckBox
android·gitee
pengyu5 小时前
【Kotlin 协程修仙录 · 大乘境 · 初阶】 | 跳出三界:协程在 KMP 与后端开发中的跨平台之道
android·kotlin
XiaoLeisj5 小时前
Android Memory Profiler:堆内存指标、内存抖动与泄漏定位
android·性能优化·内存抖动·内存泄露·memory profiler
未来猫咪花5 小时前
Everything is ViewModel:让状态管理回到对象世界
android·flutter·ios
古法安卓5 小时前
Android-重启流程源码解析
android·java·android studio