4

Currently I am programming an android app which reads out the phone number of the user. Because the app is developed for SDK 23+ (target sdk 27) I have to request the read sms permission. If the user denies the permission the first time a dialog should appear where the use of the phone number is explained. Then the user can choose to request the permission once more or to type the phone number manually. If the user tiks "never ask again" a dialog should appear where the user is instructed to allow the permission via the settings.

To check if the user has ticked never ask again, I use the method shouldShowRequestPermissionRationale. But the method always returns false, even if I never ticked never ask again.

Here is the relevant code:

public class SettingsSettingsFragment extends Fragment implements ActivityCompat.OnRequestPermissionsResultCallback {

public void readNumber() {

        if (person.getPhoneNumber() == null || person.getPhoneNumber().equalsIgnoreCase("")) {
            if (checkSelfPermission(getActivity(), READ_SMS) != PackageManager.PERMISSION_GRANTED)
                requestPermission();
            else {
//read phoneNumber
        }
    }
        //request permission send sms
private void requestPermission() {
    requestPermissions(new String[]{Manifest.permission.READ_SMS}, PERMISSION_REQUEST_SEND_SMS);
}

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == PERMISSION_REQUEST_SEND_SMS) 
            if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
                // user rejected the permission
                boolean showRationale = shouldShowRequestPermissionRationale(Manifest.permission.SEND_SMS);
                if (!showRationale) {
                    // user also CHECKED "never ask again" - show dialog
                   //show dialog: please allow in settings
                } else if (counter < 2) {
                   // explain the permission, and give the user the possibility to ask once more
                   counter++;
        }
    }

Why is shouldShowRequestPermissionRationale always returning false? The code is from here: Android M - check runtime permission - how to determine if the user checked "Never ask again"? Thanks for help in advance.

submerge
  • 51
  • 1
  • 5

2 Answers2

0

According to android developers documentation of shouldShowRequestPermissionRationale():

method 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 in the permission request system dialog, this method returns false

If you haven't requested permission before it will return false because there is no need to pop the rational alert dialog.

This answer was very helpful for me to understand the issue.

mic123456
  • 309
  • 4
  • 6
0

Your requestPermission() is for READ_SMS, butshouldShowRequestPermissionRationale() is about SEND_SMS.

shouldShowRequestPermissionRationale() keeps returning false until user is asked for the "relevant" permission. Once requested, it returns true until user denies the same permission with "Never Ask Again" checked.

So, in your case, SEND_SMS is never requested. Therefore shouldShowRequestPermissionRationale(Manifest.permission.SEND_SMS) will keep returning false as expected. I made the same mistake before.

Xfce4
  • 557
  • 5
  • 28