8

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. Users can manually deny permission in System App permissions Settings. So my question is how can I check whether my app is grand Audio permission under API level 23?

I have searched for quite a long time but only find some document about how to check permission in Android M.

I have noticed context has a function checkCallingOrSelfPermission, but it's behavior is strange under Android 6.0. I am using the solution from here. But I after I manually close app permission in System App settings. The result I get is not what I expect.

String permission = "android.permission.RECORD_AUDIO";
int ret = context.checkCallingPermission(permission);

Forget to mention:

minSdkVersion 9
targetSdkVersion 22

Edit:

I have noticed that there is a solution which is to upgrade v4 support lib to v23. It will be a little complex to upgrade my v4 support lib. So I want another solution.

Community
  • 1
  • 1
einverne
  • 6,454
  • 6
  • 45
  • 91

5 Answers5

16

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 activity
int 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

Apostrofix
  • 2,140
  • 8
  • 44
  • 71
  • I cannot use `ContextCompat.checkSelfPermission` at `compile 'com.android.support:support-v4:22.2.1'` . What can I do? – einverne May 25 '16 at 05:35
  • 3
    @einverne Oh, I just saw that you are using a target sdk version 22. There's a similar discussion with an answer here: https://stackoverflow.com/questions/33407250/checkselfpermission-method-is-not-working-in-targetsdkversion-22 – Apostrofix May 25 '16 at 05:37
  • PermissionChecker cannot resolved – einverne May 25 '16 at 05:57
  • 1
    @einverne Did you import `android.support.v4.content.PermissionChecker;` ? – Apostrofix May 25 '16 at 06:05
  • I am using `com.android.support:support-v4:22.2.1`. I don't have this class. – einverne May 25 '16 at 06:07
  • 2
    ^^ Then the best might be to upgrade to v4. I know it might be difficult but it's also a future wise decision. – Apostrofix May 25 '16 at 06:30
7

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?

ReadyFreddy
  • 898
  • 8
  • 20
1

You need to look at System Permissions and Requesting Permissions

iCurious
  • 8,161
  • 7
  • 28
  • 46
0

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

J.D.
  • 1,401
  • 1
  • 12
  • 21
0

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

enter image description here

HenryChuang
  • 1,449
  • 17
  • 28