Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check permission under API level 23? [duplicate]

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.

like image 719
einverne Avatar asked May 25 '16 05:05

einverne


People also ask

How do I check if permission is granted Android?

To check if the user has already granted your app a particular permission, pass that permission into the ContextCompat. checkSelfPermission() method. This method returns either PERMISSION_GRANTED or PERMISSION_DENIED , depending on whether your app has the permission.

How do you check auto start permission is enabled or disabled programmatically?

There's no way to find out whether the Auto-start option is enabled or not. You can manually check under Security permissions => Autostart => Enable Autostart .

What is Call_phone permission?

directly call phone numbers. Allows the app to call phone numbers without your intervention. This may result in unexpected charges or calls. Note that this doesn't allow the app to call emergency numbers.


1 Answers

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

like image 189
Apostrofix Avatar answered Oct 05 '22 04:10

Apostrofix