Android tricks – Runtime permissions and the ‘Never ask again’ option

With Android 6.0 (API 23) users are able to grant app permissions during run time. That new feature made our developer lives a bit more difficult, but gave huge powers to our users. And do you remember that little check box which says ‘Never ask me again’?
 rQqYp

Yep… this particular one! So, how can we know that this check box had ever been selected by the user? Actually, it’s not that difficult! There is a method called shouldShowRequestPermissionRationale. This method has a boolean return type. It returns:

  • true – if the app has requested this permission previously and the user denied it without selecting the ‘Never ask me again’ checkbox
  • false – if the permission was denied together with the checkbox being checked

So, if this method returns false, then we can be sure that the checkbox was selected and we can show a dialog to the user and navigate him / her to the phone settings where he / she can enable that permission.

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == REQUEST_PERMISSION) {
// for each permission check if the user granted/denied them
// you may want to group the rationale in a single dialog,
// this is just an example
for (int i = 0, len = permissions.length; i < len; i++) {
String permission = permissions[i];
if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
// user rejected the permission
boolean showRationale = shouldShowRequestPermissionRationale( permission );
if (! showRationale) {
// user also CHECKED "never ask again"
// you can either enable some fall back,
// disable features of your app
// or open another dialog explaining
// again the permission and directing to
// the app setting
} else if (Manifest.permission.WRITE_CONTACTS.equals(permission)) {
showRationale(permission, R.string.permission_denied_contacts);
// user did NOT check "never ask again"
// this is a good place to explain the user
// why you need the permission and ask if he wants
// to accept it (the rationale)
} else if ( /* possibly check more permissions...*/ ) {
}
}
}
}
}
view raw Permissions.java hosted with ❤ by GitHub

Sadly, as of this date, the official Android documentation for this method does not represent the reality. Right now, this method accepts only the String parameter. The Activity parameter is currently missing.

Resources:

https://stackoverflow.com/questions/30719047/android-m-check-runtime-permission-how-to-determine-if-the-user-checked-nev

Feel free to share, comment & give your opinion on the topic!

And only if you really REALLY liked the article, you can buy me a cup of coffee! Otherwise, don’t do it! Donate $1

 

5 thoughts on “Android tricks – Runtime permissions and the ‘Never ask again’ option

  1. How would you solve the case when user has cleared the app data, so that you don’t actually know whether permissions were requested before or not?

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s