Situation
One of my fragments accesses the camera. So of course i need to check the permission in my activity first, before i redirect to it. If the user denies the permission the Activity finishes and redirects him to the previous Activity he was in.
Additionally I would like to show a different fragment when the Activity starts and detects that the permission was permanently denied. (The little checkbox "Never ask again" on the android permission dialog)
Problem
I could not find a proper way of detecting, wether the permission was only denied once or if the "Never ask again" checkbox was checked last time and denied the permission permanently.
Keep in mind that I don't want to know that in the onRequestPermissionsResult callback. I need to know in the onCreate of my Activity if the permission currently is granted, denied or permanently denied.
What I triedActivityCompat#shouldShowRequestPermissionRationale
seems to detect wether the permission has been denied in the past or not. It also returns true if it has been denied only once instead of permanently.
PermissionChecker#checkPermission()
didn't seem to notice any difference between permanently and only once denied permission state.
Question
Is there any way of detecting, wether a permission is denied but can still be requested or if it is permanently denied?
To know if the user denied with "never ask again" you can check again the shouldShowRequestPermissionRationale method in your onRequestPermissionsResult when the user did not grant the permission. You can open your app setting with this code: Intent intent = new Intent(Settings.
To ask permission, we need to use ActivityCompat. requestPermission() method with the permission name and the request code.
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.
So It is easy to detect that user yet not given permission even asked before but it’s tricky to define whether user has denied permission permanently or app never requested permission to user.
In the User/Group field, enter the user's name, and then select Check Now. Review the permissions the user has on the site, and through which security group (if applicable). If the user doesn't have appropriate permissions, grant the user permissions to the file or site.
If denied permission for ever, then returns false . And the trick is that, even the user allowed permission then then shouldShowRequestPermissionRationale will return false. So we can combine both the conditions to get the never ask again chosen or not.
Access Denied You need permission to access this site User not found in the directory Cause There are many scenarios which can prompt one of these messages. The most frequent cause is that permissions for the user or administrator are configured
shouldShowRequestPermissionRationale
returns true or false based on the user preference in previous permission request.
If user just denied permission(not forever) shouldShowRequestPermissionRationale
will return true
.
If denied permission for ever, then returns false
.
And the trick is that, even the user allowed permission then then shouldShowRequestPermissionRationale
will return false
.
So we can combine both the conditions to get the never ask again chosen or not.
So if user not allowed permission and shouldShowRequestPermissionRationale
returns false
then it means user opt for never ask again for the permission.
Is there any way of detecting, whether a permission is denied but can still be requested or if it is permanently denied?
Unfortunately there is no official API available to detect if permission is permanently denied when user selects "Never ask again"
There is one work around which uses shouldShowRequestPermissionRationale
. Create a SharedPreference with default value false
and store value returned by shouldShowRequestPermissionRationale
in it. Before updating the value, check if the value set was true
. If it was true
then don't update it.
Whenever you want to check for permission, get the value from SharedPreference
and current value returned by shouldShowRequestPermissionRationale
. If shouldShowRequestPermissionRationale
returns false
but value from SharedPreference
is true
, you can deduce that Never ask again
was selected by user.
You can refer to my blog where I have described this approach.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With