Reputation: 145
I want to request permissions on runtime. I checked out official android developer website and it says that shouldShowRequestPermissionRationale
returns true if permission was previously denied and returns false if permission has been denied AND never ask again checkbox was selected.
Then I saw this code in the site:
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.READ_CONTACTS)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed; request the permission
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
My 2 questions are:
What happens if the user wasn't asked permission previously? We need to ask the user, right? Where do you put that code?
The above code asks for permission even when the user checked the never ask again checkbox (when shouldShowRequestPermissionRationale
returns false, I.e., in the else block). How can you ask for permission when the user has checked that option?
Upvotes: 6
Views: 9973
Reputation: 576
To answer both your questions:
You can first use checkSelfPermission()
to see if the permission has already been granted. If it has not been granted then you should check if shouldShowRequestPermissionRationale()
returns true or false.
shouldShowRequestPermissionRationale()
will return true in the following case:
shouldShowRequestPermissionRationale()
will return false in the following 2 cases:
So, what you can do is, if shouldShowRequestPermissionRationale()
returns false
you can use a boolean preference value (default value as true) to check for
first time request of permission in the else case, if it is the first
request, then trigger requestPermissions
else if it is not the first request and the user has previously denied the request and also has checked the "Never ask again" checkbox, you can show a simple toast with the reason for unavailability of the feature that requires the permission and also mention the steps to manually enable it via settings.
Something like this:
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.READ_CONTACTS)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
if(isFirstTimeRequest){
// No explanation needed; request the permission
// RESET PREFERENCE FLAG
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
} else {
// User denied previously and has checked "Never ask again"
// show a toast with steps to manually enable it via settings
}
}
Upvotes: 23