Can't get BIND_NOTIFICATION_LISTENER_SERVICE permission for NotificationListenerService

I created an application and added a java class that extends "NotificationListenerService". Everything should work fine, but I just can't get BIND_NOTIFICATION_LISTENER_SERVICE permission. I added it to my manifest, but when I check the permission with:

        if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE)
            != PackageManager.PERMISSION_GRANTED)
    {
        Log.d(TAG, "permission denied");
    }
    else
        Log.d(TAG, "permission granted");

      

but I refuse permission all the time. I know this permission is not a "dangerous permission", so I don't have to ask the user for it. In the manifest, I said the following:

        <service android:name=".PcNotification"
        android:label="PCNotificationService"
        android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
        <intent-filter>
            <action android:name="android.service.notification.NotificationListenerService" />
        </intent-filter>
    </service>

      

and it still doesn't work. I also implemented the function onListenerConnected

, but it was never called, so this means my service never connects to the notification manager, possibly because I don't have the BIND_NOTIFICATION_LISTENER_SERVICE permission

I just want to know how to grant this permission to my application.

+3


source to share


2 answers


I just cant get BIND_NOTIFICATION_LISTENER_SERVICE permission

You don't need to ask for this permission. You need to secure your service with this permission. You do this on an element <service>

using an attribute android:permission

.



I know this permission is not a "dangerous permission", so I don't have to ask the user for it.

Then get rid of the code prompting the user for it. It is unnecessary and it will not work.

+2


source


This should be a literal answer to the original question. However, if you want the method below, you have misunderstood the use of such services (as I did). You don't have to start your notification listener yourself, you just need to check if your service is running, and if not, then you already know that the permission was not granted and you can point the user to the settings panel in android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS

.

Add this method to your NotificationListenerClass and it will return true if permission is granted, false if not:



public Boolean VerifyNotificationPermission() {
    String theList = android.provider.Settings.Secure.getString(getContentResolver(), "enabled_notification_listeners");
    String[] theListList = theList.split(":");
    String me = (new ComponentName(this, YourNotificationListenerClass.class)).flattenToString();
    for ( String next : theListList ) {
        if ( me.equals(next) ) return true;
    }
    return false;
}

      

The "enabled_notification_listeners" line appears to be defined under the hood in Settings.Secure.ENABLED_NOTIFICATION_LISTENERS

, but I can't seem to resolve it, so I'm using a possibly not well-maintained literal string. If anyone knows how to get it from the link, please add it / edit!

0


source







All Articles