Permissions icon indicating copy to clipboard operation
Permissions copied to clipboard

ContextCompat.checkSelfPermission method won't work on pre-Marshmallow devices

Open hwangjr opened this issue 10 years ago • 0 comments

I read your post:

It is worth re-iterating what ContextCompat does for us here. The checkSelfPermission() method will always return PackageManger.PERMISSION_GRANTED when running on pre-Marshmallow devices which do not support the new runtime permissions model – the permissions are implicitly granted on older OS levels, so we simply have one method call which works across all OS levels because of the Manifest declaration, and we don’t need to write any API-level specific checks in to our code.

so i wonder why don't just use the package manager to check permission:

PackageManager.PERMISSION_GRANTED == context.getPackageManager().checkPermission(permission, context.getPackageName());

or :

public static boolean lacksPermission(Context context, String permission) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            return ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_DENIED;
        } else {
            return PackageManager.PERMISSION_GRANTED == context.getPackageManager().checkPermission(permission, context.getPackageName());
        }
    }

hwangjr avatar Jan 07 '16 02:01 hwangjr