Skip to content

Instantly share code, notes, and snippets.

Last active September 11, 2017 13:34
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Listen for when your app is allowed or denied to listen for notifications
* This is just a simple ContentObserver class used to listen for
* changes to the list of enabled notification listeners.
* You'll need to create an instance of this class and call observe()
* to listen for changes and unobserve() when you no longer need it.
class ListenerStatusObserver extends ContentObserver {
* Note: Settings.Secure.ENABLED_NOTIFICATION_LISTENERS is hidden from
* the public API so you'll want to replace it with the actual string
* which is "enabled_notification_listeners"
public ListenerStatusObserver() {
super(new Handler());
public void observe() {
ContentResolver cr = getContentResolver();
Settings.Secure.ENABLED_NOTIFICATION_LISTENERS), false, this);
public void unobserve() {
public void onChange(boolean selfChange) {
public void update() {
final String flat = Settings.Secure.getString(getContentResolver(),
boolean enabled = false;
if (flat != null && !"".equals(flat)) {
final String[] names = flat.split(":");
for (int i = 0; i < names.length; i++) {
if (names[i].contains(getPackageName())) {
enabled = true;
// Do whatever you want based on the value of enabled
Toast.makeText(NotificationListener.this, TAG + " enabled=" + enabled, Toast.LENGTH_SHORT).show();
Copy link

Hey. How can I set enable to a specific or all the applications that ask for notification access?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment