gpt4 book ai didi

java - 如何检查,如果用户授予 BIND_NOTIFICATION_LISTENER_SERVICE 权限

转载 作者:行者123 更新时间:2023-12-04 23:54:17 27 4
gpt4 key购买 nike

我是 Android 的新手,我正在尝试开发一个应用程序,它读取 Whatsapp 通知并用它们做一些事情:)

我尝试了不同的方法来检查用户是否授予了“BIND_NOTIFICATION_LISTENER_SERVICE”权限但没有任何效果。它总是说,未授予许可。但事实并非如此。这是代码:

        if(ContextCompat.checkSelfPermission(this, Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE) == PackageManager.PERMISSION_GRANTED){
Log.i(TAG, "App has permission!");
} else
Log.i(TAG, "App hasn't permission " + ContextCompat.checkSelfPermission(this, Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE));

这是我在 App 中更改的设置:startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));

list :

            android:label="Whatsapp Nachrichten leser"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>

Listener-Service 完美运行。它只是说,我没有权限。(抱歉我的英语不好)

最佳答案

这是解决方案:

private boolean isNotificationServiceEnabled(Context c){
String pkgName = c.getPackageName();
final String flat = Settings.Secure.getString(c.getContentResolver(),
"enabled_notification_listeners");
if (!TextUtils.isEmpty(flat)) {
final String[] names = flat.split(":");
for (int i = 0; i < names.length; i++) {
final ComponentName cn = ComponentName.unflattenFromString(names[i]);
if (cn != null) {
if (TextUtils.equals(pkgName, cn.getPackageName())) {
return true;
}
}
}
}
return false;
}

关于java - 如何检查,如果用户授予 BIND_NOTIFICATION_LISTENER_SERVICE 权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63834256/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com