Accessing Android NotificationListenerService Settings

powerj1984 picture powerj1984 · Jul 25, 2013 · Viewed 14.6k times · Source

Android has a new notification listener service as of 4.3: http://developer.android.com/about/versions/jelly-bean.html http://developer.android.com/reference/android/service/notification/NotificationListenerService.html

From the docs:

Notification access is disabled by default — apps can use a new Intent to take the user directly to the Settings to enable the listener service after installation.

I don't see the intent to fire documented anywhere. Perusing the Settings doc doesn't seem helpful: http://developer.android.com/reference/android/provider/Settings.html

Looking at the Settings class directly: https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/provider/Settings.java

I see ACTION_NOTIFICATION_LISTENER_SETTINGS defined, but when using Android Studio and pointing at 4.3 ACTION_NOTIFICATION_LISTENER_SETTINGS can't be resolved:

Intent intent = new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS);

Trying it more manually doesn't seem to work:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName("com.android.settings", "android.settings.NOTIFICATION_LISTENER_SETTINGS");

edit: doing it the correct way as CommonsWare pointed out below:

Intent intent=new Intent("android.settings.NOTIFICATION_LISTENER_SETTINGS");

leads to a crash:

(android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.settings.NOTIFICATION_LISTENER_SETTINGS })

Am I missing something? I'm not sure how to send the user to the proper settings screen to enable this service in my app.

Answer

CommonsWare picture CommonsWare · Jul 25, 2013

Am I missing something?

Well, in your last one, you are conflating an action string with a class name. The "manual" approach would be:

Intent intent=new Intent("android.settings.NOTIFICATION_LISTENER_SETTINGS");

In terms of why Android Studio is not finding Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS, that I can't say.


UPDATE

Based on the discussion in the comments, Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS is not in the Android SDK at present (marked with @hide). Also, the manifest for the Settings app has a slightly different version of the action string:

Intent intent=new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");