Steve2955
Steve2955

Reputation: 690

ContextCompat.checkSelfPermission() returns PERMISSION_DENIED although Permission is granted

I'm trying to get the BIND_NOTIFICATION_LISTENER_SERVICE permission granted by the user. To do that I'm opening the settings app at the correct spot using:

Intent intent = new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS);
startActivity(intent);

What's kinda weird here already is that the Settings are opened twice(if you press the back button once, the same settings screen opens again)

However, in onResume() I then check if the permission has been granted using:

if(ContextCompat.checkSelfPermission(context,Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE)== PackageManager.PERMISSION_GRANTED){
   //open next activity
}

And now here is the issue: It doesn't matter if the user granted the permission in the settings, because checkSelfPermission() always returns PERMISSION_DENIED.

And now it gets really weird: my NotificationListenerService is instantiated, bound and fully working although the permission hasn't been granted according to checkSelfPermission().

How am I supposed to know if the user granted the permission?

Permission and Service declaration in my Manifest:

<uses-permission android:name="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" />

    <application>      
        <service
            android:name=".service.NotificationListener"
            android:directBootAware="true"
            android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
            <intent-filter>
                <action android:name="android.service.notification.NotificationListenerService" />
            </intent-filter>
        </service>
    </application>

My NotificationListenerService:

public class NotificationListener extends NotificationListenerService {

    private static final String TAG = NotificationListener.class.getSimpleName();

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        super.onNotificationPosted(sbn);
        Log.d(TAG, "onNotificationPosted: "+sbn.getNotification().tickerText + " ;" + sbn.getPackageName());
    }
}

What I already tried:

Upvotes: 1

Views: 885

Answers (1)

R. Zag&#243;rski
R. Zag&#243;rski

Reputation: 20268

According to documentation permission BIND_NOTIFICATION_LISTENER_SERVICE has:

Protection level: signature

This means, that only system applications can have it and use it. Asking for permissions by the way you defined is possible only, when the permission has:

Protection level: dangerous

If your app is not a system app and you have not rooted the device to have the possibility to install custom ROM on it with you custom certificate (your app must be signed with it, too), then you just can't get this permission.

Upvotes: 0

Related Questions