Android 7.0/API24: How to check for notification a

2019-09-03 16:25发布

问题:

In Android 6.0/API23 and earlier, the following used to work:

String settingEnabled = android.provider.Settings.Secure.getString(this.getContentResolver(), "enabled_notification_listeners");

In Android 7.0 Nougat/API24 this seems to be no longer supported, because the code above returns null.

It actually was never mentioned here: https://developer.android.com/reference/android/provider/Settings.Secure.html

How do we check if our app has notification access in Android 7.0 Nougat API24?

Edit: It seems that actually that after you first gained the access in the settings, the code above returns the correct state. But not on the initial request after installation.

回答1:

Use this:

Set<String> packageNames = NotificationManagerCompat.getEnabledListenerPackages (context);