Checking Android System Security Notification Access setting programmatically

陌路散爱 提交于 2020-01-01 10:15:19

问题


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


来源:https://stackoverflow.com/questions/24145343/checking-android-system-security-notification-access-setting-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!