如何检查用户是否已授予BIND_NOTIFICATION_LISTENER_SERVICE权限

huangapple go评论66阅读模式
英文:

How to check, if user granted BIND_NOTIFICATION_LISTENER_SERVICE Permission

问题

我是Android的新手,正在尝试开发一个应用程序,用于读取WhatsApp通知并对其执行某些操作 如何检查用户是否已授予BIND_NOTIFICATION_LISTENER_SERVICE权限

我尝试了不同的方法来检查用户是否授予了"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 &quot;BIND_NOTIFICATION_LISTENER_SERVICE&quot;
But nothing worked. It always said, that the permission isn&#39;t granted. But that isn&#39;t true. Here is the code:
```//Checke Permissions
        if(ContextCompat.checkSelfPermission(this, Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE) == PackageManager.PERMISSION_GRANTED){
            Log.i(TAG, &quot;App has permission!&quot;);
        } else
            Log.i(TAG, &quot;App hasn&#39;t permission &quot; + ContextCompat.checkSelfPermission(this, Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE));

That are the settings, which I changed in the App:
startActivity(new Intent(&quot;android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS&quot;));

Manifest:

            android:label=&quot;Whatsapp Nachrichten leser&quot;
            android:permission=&quot;android.permission.BIND_NOTIFICATION_LISTENER_SERVICE&quot;&gt;
            &lt;intent-filter&gt;
                &lt;action android:name=&quot;android.service.notification.NotificationListenerService&quot; /&gt;
            &lt;/intent-filter&gt;
        &lt;/service&gt;

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(),
                &quot;enabled_notification_listeners&quot;);
        if (!TextUtils.isEmpty(flat)) {
            final String[] names = flat.split(&quot;:&quot;);
            for (int i = 0; i &lt; 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)

huangapple
  • 本文由 发表于 2020年9月11日 00:49:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63834256.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定