Search code examples
androidpermissionsaccessibility

Is there any option to check the status of accessibility permission in Android (granted to the app or not)


Is there any option to check the status of accessibility permission in Android, whether it is granted to the app or not?

I tried both (from this post)

private boolean checkWriteExternalPermission()
{
    String permission =  Manifest.permission.BIND_ACCESSIBILITY_SERVICE;
    int res = getContext().checkCallingOrSelfPermission(permission);
    return (res == PackageManager.PERMISSION_GRANTED);            
}

and

PackageManager pm = context.getPackageManager();
int hasPerm = pm.checkPermission(
    android.Manifest.permission.BIND_ACCESSIBILITY_SERVICE, 
    context.getPackageName());
if (hasPerm != PackageManager.PERMISSION_GRANTED) {
   // do stuff
}

but in both cases, the result is negative irrespective of permission status. Even if the service is using the permission, the result is -1.

Please check the comments for the correct answer, as the question can not receive answers while being closed. Thanks to abdu and Duna.


Solution

  • Thanks to Duna and Abdu for the answers they dropped on the comments as the question was closed once.

    from the comment posted by Abdu

    public static boolean isAccessibilityEnabled(Context context, String id) { 
        AccessibilityManager am = (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);    
        List<AccessibilityServiceInfo> runningServices = am.getEnabledAccessibilityServiceList(AccessibilityEvent.TYPES_ALL_MASK); for         
    
        (AccessibilityServiceInfo service : runningServices) { 
            if(id.equals(service.getId())) { 
                return true; 
            } 
        } 
            return false; 
        }
    

    and from link provided by Duna
    github.com/parthdave93/AccessibilityServiceExample

    public static boolean isAccessibilitySettingsOn(Context mContext) {
    int accessibilityEnabled = 0;
    //your package /   accesibility service path/class
    final String service = "com.example.sotsys_014.accessibilityexample/com.accessibilityexample.Service.MyAccessibilityService";
    
    boolean accessibilityFound = false;
    try {
        accessibilityEnabled = Settings.Secure.getInt(
                mContext.getApplicationContext().getContentResolver(),
                android.provider.Settings.Secure.ACCESSIBILITY_ENABLED);
        Log.v(TAG, "accessibilityEnabled = " + accessibilityEnabled);
    } catch (Settings.SettingNotFoundException e) {
        Log.e(TAG, "Error finding setting, default accessibility to not found: "
                + e.getMessage());
    }
    TextUtils.SimpleStringSplitter mStringColonSplitter = new TextUtils.SimpleStringSplitter(':');
    
    if (accessibilityEnabled == 1) {
        Log.v(TAG, "***ACCESSIBILIY IS ENABLED*** -----------------");
        String settingValue = Settings.Secure.getString(
                mContext.getApplicationContext().getContentResolver(),
                Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
        if (settingValue != null) {
            TextUtils.SimpleStringSplitter splitter = mStringColonSplitter;
            splitter.setString(settingValue);
            while (splitter.hasNext()) {
                String accessabilityService = splitter.next();
    
                Log.v(TAG, "-------------- > accessabilityService :: " + accessabilityService);
                if (accessabilityService.equalsIgnoreCase(service)) {
                    Log.v(TAG, "We've found the correct setting - accessibility is switched on!");
                    return true;
                }
            }
        }
    } else {
        Log.v(TAG, "***ACCESSIBILIY IS DISABLED***");
    }
    
    return accessibilityFound;
    }