Checking Android System Security Notification Acce

2019-04-07 13:28发布

问题:

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);

回答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



回答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");


回答3:

Keeping the status in a static variable from onBind() and onUnbind() worked for me.

public boolean onUnbind(Intent mIntent) {
    boolean mOnUnbind = super.onUnbind(mIntent);
    Common.isNotificationAccessEnabled = false;
    return mOnUnbind;
}

public IBinder onBind(Intent mIntent) {
    IBinder mIBinder = super.onBind(mIntent);
    Common.isNotificationAccessEnabled = true;
    return mIBinder;
}