java – Is there any option to check the status of accessibility permission in android (granted to the app or not)-ThrowExceptions

Exception or error:

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

How to solve:

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
        }

    } 

Leave a Reply

Your email address will not be published. Required fields are marked *