Skip to content Skip to sidebar Skip to footer

How Can I Check Permission Under Api Level 23?

One of my app has a permission RECORD_AUDIO and my app's targetSdkVersion is 22. I cannot check permission at runtime. But I notice if I install my app in Android 6.0 and above. Us

Solution 1:

In API 22 and below:

int permission = PermissionChecker.checkSelfPermission(context, permission);

if (permission == PermissionChecker.PERMISSION_GRANTED) {
    // good to go
} else {
    // permission not granted, you decide what to do
}

In API 23 and above:

From the docs:

If your app needs a dangerous permission, you must check whether you have that permission every time you perform an operation that requires that permission. The user is always free to revoke the permission, so even if the app used the camera yesterday, it can't assume it still has that permission today.

To check if you have a permission, call the ContextCompat.checkSelfPermission() method. For example, this snippet shows how to check if the activity has permission to write to the calendar:

// Assume thisActivity is the current activityint permissionCheck = ContextCompat.checkSelfPermission(thisActivity,
        Manifest.permission.WRITE_CALENDAR);

If the app has the permission, the method returns PackageManager.PERMISSION_GRANTED, and the app can proceed with the operation. If the app does not have the permission, the method returns PERMISSION_DENIED, and the app has to explicitly ask the user for permission.

https://developer.android.com/training/permissions/requesting.html

Solution 2:

for Api 22 and lower versions, if i am not wrong, you don't need to check permission on runtime, you have to just add them at AndroidManifest and app will ask this permission just one time when first install period. So it's maybe you have to check your AndroidManifest, can you please add this to here.

In case of you want to use your application on Api 23 as well as lower versions:

You can try to use if statement to check Sdk version and request dungerous permission on runtime if version 23 or upper.

if (android.os.Build.VERSION.SDK_INT > 23) { /*Ask Dungerous Permissions here*/ }

and maybe it will solve the problem on Api 22 and lower versions.

You can check how to ask permission on runetime from Requesting Permissions at Run Time

Also you have to add Api 23 compile to your application and make your target to Api 23.

Edit : Now i see you mentioned that, it's complex to upgrade your support lib to v23 but i didn't understand why. You need to just install some SDK and add some Strings to gradle. Do you use something like custom support lib?

Solution 3:

Solution 4:

Please Go through this link will guide you how permission is set and reduce your work to implement permission

Solution 5:

Example of Dangerous Permissions and Special Permissions https://github.com/henrychuangtw/AndroidRuntimePermission

enter image description here

Post a Comment for "How Can I Check Permission Under Api Level 23?"