英文:
How to check, if user granted BIND_NOTIFICATION_LISTENER_SERVICE Permission
问题
我是Android的新手,正在尝试开发一个应用程序,用于读取WhatsApp通知并对其执行某些操作
我尝试了不同的方法来检查用户是否授予了"BIND_NOTIFICATION_LISTENER_SERVICE"权限,但是没有任何效果。它总是显示权限未被授予,但事实并非如此。以下是代码部分:
if (ContextCompat.checkSelfPermission(this, Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE) == PackageManager.PERMISSION_GRANTED) {
Log.i(TAG, "应用已获得权限!");
} else
Log.i(TAG, "应用未获得权限 " + ContextCompat.checkSelfPermission(this, Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE));
以下是我在应用程序中更改的设置:
startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
清单文件:
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>```
监听服务已经完美运行。它只是显示我没有权限。
(对于我的糟糕英语表示抱歉)
<details>
<summary>英文:</summary>
I am new to Android and I am trying to develop an app, which reads Whatsapp Notifications and does something with them :)
I tried different things, to check, if user granted the permission "BIND_NOTIFICATION_LISTENER_SERVICE"
But nothing worked. It always said, that the permission isn't granted. But that isn't true. Here is the code:
```//Checke Permissions
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));
That are the settings, which I changed in the App:
startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
Manifest:
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>
The Listener-Service works perfectly. It only sais, I don't have the permission.
(Sorry for my bad english)
答案1
得分: 7
这是解决方案:
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;
}
英文:
This is the solution:
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;
}
答案2
得分: 3
谷歌为此提供了官方的 API:
NotificationManagerCompat.getEnabledListenerPackages(context).contains(context.packageName)
英文:
Google offers an official API for this:
NotificationManagerCompat.getEnabledListenerPackages(context).contains(context.packageName)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论