Michael Michael - 1 year ago 71
Android Question

Determine whether a permission is present in the manifest with Android 23+

I am creating a library that needs to check runtime permissions. I have got runtime permissions working fine and understand the use cases without issues.

However I would like to confirm that the developer using our library has added the permission to their manifest.

The library is a location based library and the developer can either enter

into the manifest and I need to be able to determine which they have used (or both) at runtime.

I though using the package manager to check permission would work however this always seems to fail:

PackageManager pm = getPackageManager();
int granted = pm.checkPermission(
getPackageName() );
if (granted == PackageManager.PERMISSION_GRANTED)
// Use coarse for runtime requests
// granted is always PackageManager.PERMISSION_DENIED

Is there some other way to do this in Android v23+?

Answer Source

Off the cuff, retrieve the PackageInfo via PackageManager and getPackageInfo(getPackageName(), PackageManager.GET_PERMISSIONS). Then, look at the requestedPermissions array in the PackageInfo for all the <uses-permission>-requested permissions.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download