Listen for when your app is allowed or denied to listen for notifications
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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(); | |
cr.registerContentObserver(Settings.Secure.getUriFor( | |
Settings.Secure.ENABLED_NOTIFICATION_LISTENERS), false, this); | |
update(); | |
} | |
public void unobserve() { | |
getContentResolver().unregisterContentObserver(this); | |
} | |
@Override | |
public void onChange(boolean selfChange) { | |
update(); | |
} | |
public void update() { | |
final String flat = Settings.Secure.getString(getContentResolver(), | |
Settings.Secure.ENABLED_NOTIFICATION_LISTENERS); | |
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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey. How can I set enable to a specific or all the applications that ask for notification access?