sumanth
sumanth

Reputation: 145

Working of shouldShowRequestPermissionRationale method

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:

Upvotes: 6

Views: 9973

Answers (1)

raxerz
raxerz

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:

  • When the user has denied the permission previously but has not checked the "Never Ask Again" checkbox.

shouldShowRequestPermissionRationale() will return false in the following 2 cases:

  • When the user has denied the permission previously AND never ask again checkbox was selected.
  • When the user is requesting permission for the first time.

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

Related Questions