androidandroid-13android-tiramisu

How check if ACCESS_RESTRICTED_SETTINGS is enabled?


Is there some Java code to check if the ACCESS_RESTRICTED_SETTINGS permission is enabled?

enter image description here


Solution

  • public static boolean hasAccessRestrictedPerm(Context context) {
        try {
            PackageManager packageManager = context.getPackageManager();
            ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
            AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
            int mode = appOpsManager.checkOpNoThrow("android:access_restricted_settings", applicationInfo.uid, applicationInfo.packageName);
            return (mode == AppOpsManager.MODE_ALLOWED);
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
    }