问题
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.
回答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 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
回答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?
回答3:
You need to look at System Permissions and Requesting Permissions
回答4:
Please Go through this link will guide you how permission is set and reduce your work to implement permission
回答5:
Example of Dangerous Permissions and Special Permissions https://github.com/henrychuangtw/AndroidRuntimePermission
来源:https://stackoverflow.com/questions/37428464/how-can-i-check-permission-under-api-level-23