I was going through the official doc about the new Permissions model in Android M. It talks about the shouldShowRequestPermissionRationale()
function which returns true
if the app has requested this permission previously and the user denied the request. If the user turned down the permission request in the past and chose the Don't ask again option, this method returns false
.
But how can we differentiate between the following two cases?
Case 1: The app doesn't have a permission and the user has not been asked for the permission before. In this case, shouldShowRequestPermissionRationale() will return false because this is the first time we're asking the user.
Case 2: The user has denied the permission and selected "Don't ask again", in this case too shouldShowRequestPermissionRationale() will return false.
I would want to send the user to the App's settings page in Case 2. How do i go about differentiating these two cases?
If anyone is interested in a Kotlin solution, I refactored @muthuraj answer to be in Kotlin. Also modernized it a bit to have a completion block instead of listeners.
PermissionUtil
PermissionHandler
Implementation
Just post another option, if anyone may feel like. You can use EasyPermissions which was provided by Google itself, to, as said, "Simplify Android M system permissions".
Then you don't have to handle
shouldShowRequestPermissionRationale
directly.After M Preview 1, if the dialog is displayed for the first time, there is no Never ask again checkbox.
If the user denies the permission request, there will be a Never ask again checkbox in the permission dialog the second time permission is requested.
So the logic should be like this:
Request permission:
Check if the permission was denied or granted in
onRequestPermissionsResult
.If the permission was denied previously, this time there will be a Never ask again checkbox in the permission dialog.
Call
shouldShowRequestPermissionRationale
to see if the user checked Never ask again.shouldShowRequestPermissionRationale
method returns false only if the user selected Never ask again or device policy prohibits the app from having that permission:So, you won't have to track if a user checked Never ask again or not.
This code asks user to ask permission during runtime, if user allows, it execute result method, if user deny, it ask again with discription with user deny (it ask again with instructions), but if user choose never ask again. it handles never ask again, display open settings option with instructions.
May be useful for someone:--
What I have noticed is, if we check the shouldShowRequestPermissionRationale() flag in to onRequestPermissionsResult() callback method, it shows only two states.
State 1:-Return true:-- Any time user clicks Deny permissions (including the very first time).
State 2:-Returns false :- if user selects “never asks again".
Link for detailed working example.