0

Is there any option to check the status of accessibility permission in android ( whether it is granted to the app or not ).

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 status of the permission . even if the service is using the permission result is -1

Please check the comments for correct answer, it is no longer able to receive answers. thanks to abdu and Duna

Harsh
  • 319
  • 3
  • 11
  • `ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.WRITE_CALENDAR);` [read](https://developer.android.com/training/permissions/requesting.html) – Pavneet_Singh Oct 16 '17 at 16:10
  • 1
    I have tried this , but it returns -1 irrespective of the status (i used Manifest.permission.BIND_ACCESSIBILITY_SERVICE) – Harsh Nov 03 '17 at 16:38
  • 1
    public static boolean isAccessibilityEnabled(Context context, String id) { AccessibilityManager am = (AccessibilityManager) context .getSystemService(Context.ACCESSIBILITY_SERVICE); List runningServices = am.getEnabledAccessibilityServiceList(AccessibilityEvent.TYPES_ALL_MASK); for (AccessibilityServiceInfo service : runningServices) { if (id.equals(service.getId())) { return true; } } return false; } – Abdu Sep 18 '18 at 22:50
  • Could you post this as an answer please – Harsh Nov 24 '18 at 22:47

1 Answers1

-1

You can use following code for checking the permission status to your app. (here for write permission to external storage).

if(checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)
            {
                //Permission Granted
                Log.v("LOG", "Permission is granted ");
                return true;
            }
            else{
                //Request For Permission
                Log.v("LOG","Permission is Revoked");
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
                return false;
            }

If the permission is revoked then use this override method

@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if(grantResults[0] == PackageManager.PERMISSION_GRANTED)
        {
            Log.v("LOG", "Permission: " + permissions[0] + "was" + grantResults[0]);
            //resume task needing this permission
        }

    } 
ap14
  • 3,791
  • 1
  • 14
  • 26