Checking Android System Security Notification Access setting programmatically

11,577

Solution 1

Make it simple

  if (Settings.Secure.getString(this.getContentResolver(),"enabled_notification_listeners").contains(getApplicationContext().getPackageName())) 
    {
        //service is enabled do something
    } else {
        //service is not enabled try to enabled by calling...
       getApplicationContext().startActivity(new Intent(
            "android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
    }

'else' part is optional according your question

Solution 2

To judge whether Android Settings Notification contains your app or not, you should do:

import android.provider.Settings;

String enabledAppList = Settings.Secure.getString(
        this.getContentResolver(), "enabled_notification_listeners");
boolean temp = enabledAppList.contains("youAppName");
Share:
11,577
AndroidDev
Author by

AndroidDev

Software Maker Software Developer Software Designer Software Tester Software User

Updated on July 28, 2022

Comments

  • AndroidDev
    AndroidDev almost 2 years

    Is there a way to check whether the below setting is enabled or disabled programmatically in Android?

    Settings > Security > Device Administration > Notification Access > My App 
    

    Currently I'm invoking the settings dialog from my app through the below method but if the settings are already enabled then I dont want to present this settings.

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