1

From the doc, shouldShowRequestPermissionRationale indicates whether you should show permission rationale UI.

enter image description here

I found:

  1. if change one permission to "Notify" and before first showing the permission-request-dialog, shouldShowRequestPermissionRationale return false
  2. if deny the permission with the "Never ask again" checkbox selected, shouldShowRequestPermissionRationale return false

Then I met a problem, how to distinguish these two cases? I mean, in the following code snippet from Android developer:

if (ContextCompat.checkSelfPermission(
    CONTEXT, Manifest.permission.REQUESTED_PERMISSION) ==
    PackageManager.PERMISSION_GRANTED) {
    // You can use the API that requires the permission.
    performAction(...);
} else if (shouldShowRequestPermissionRationale(...)) {
    // In an educational UI, explain to the user why your app requires this
    // permission for a specific feature to behave as expected. In this UI,
    // include a "cancel" or "no thanks" button that allows the user to
    // continue using your app without granting the permission.
    showInContextUI(...);
} else {
    // You can directly ask for the permission.
    requestPermissions(CONTEXT,
            new String[] { Manifest.permission.REQUESTED_PERMISSION },
            REQUEST_CODE);
}

In the case 2, though denied the permission with "Never ask again", the code still invokes requestPermissions?

mianlaoshu
  • 2,342
  • 3
  • 27
  • 48
  • Does this answer your question? [Android M Permissions : Confused on the usage of shouldShowRequestPermissionRationale() function](https://stackoverflow.com/questions/32347532/android-m-permissions-confused-on-the-usage-of-shouldshowrequestpermissionrati) – Agent_L Aug 18 '23 at 12:51

1 Answers1

0

Method shouldShowRequestPermissionRationale() returns a boolean value.

Returns True if the the permission asked before but the user denied without checking ‘Never ask again’

Returns False in two cases.

1- If the permission is requested first time.

2- If the permission asked before but the user denied with checking ‘Never ask again’.

in your case We can just put a flag on Shared Preferences for checking the permission is being asked first time or not.

code

if (ContextCompat.checkSelfPermission(thisActivity,Manifest.permission.READ_CONTACTS)!= PackageManager.PERMISSION_GRANTED) {
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
            Manifest.permission.READ_CONTACTS)) {
        // Show an expanation to the user
    } else {
         if(isFirstTimeAsking(thisActivity, Manifest.permission.READ_CONTACTS){
               firstTimeAsking(thisActivity,Manifest.permission.READ_CONTACTS, false);
               ActivityCompat.requestPermissions(thisActivity,new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA); 
         } else {
               //Permission disable by device policy or user denied permanently. Show proper error message
                }
    }
} else {
  //permission granted. do your stuff
}

i hope that find solution