Correct usage of permission application shouldShowRequestPermissionRationale

When does the method return true

What is the function of the shouldShowRequestPermissionRationale method? It is easy to understand from the naming. It is to judge whether it is necessary to display the permission description pop-up window to the user.

For a better understanding, first look at a piece of Google's sample code

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.
    // The registered ActivityResultCallback gets the result of this request.
    requestPermissionLauncher.launch(
            Manifest.permission.REQUESTED_PERMISSION);
}

Running this code can be divided into three situations

The first stage

I have never applied for permission, and the third code block is executed, and the system permission pop-up box pops up. At this time shouldShowRequestPermissionRationale returns false.

second stage

After the permission pop-up box popped up in the first stage, we chose to deny the authorization permission. Then run this code again. At this time, the second code block is executed, and our custom permission instruction pop-up window pops up. At this time shouldShowRequestPermissionRationale returns true.

The third phase

In the first two stages, we denied the authorization permission and ran this code again. At this time, the third code block was executed. shouldShowRequestPermissionRationale returns false, but this time the system permission box does not pop up. Indicates that the system has prohibited the application for this permission.

Summarize

shouldShowRequestPermissionRationale will return ture only after the first rejection, and return false for the second rejection

How to judge that the permission is forbidden

The official does not provide the corresponding API. Combining the above logic, we can conclude that

if (grantResult == PackageManager.PERMISSION_DENIED 
       && !shouldShowRequestPermissionRationale(Manifest.permission_group.STORAGE)){
                //权限被禁止了
 }

Note that it can only be judged in onRequestPermissionsResult. Because shouldShowRequestPermissionRationale returns false without applying for permission for the first time, it is in line with this judgment.

The actual application process

There are differences between the requirements defined by daily products and the templates defined by Google:

1. The permission instruction pop-up window is before the first application, not the second application

2. After being banned, the user will be guided to the setting page to open it manually

So the correct flow:

private void requestPemission() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            start();
        } else {
            if (ContextCompat.checkSelfPermission(Utils.getApp(), Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    == PackageManager.PERMISSION_GRANTED) {
                start();
            } else if (isFirstRequst) {
                //说明弹窗
                showTipDialog();
            } else {
                //申请权限
                requestPermissions(new String[]{Manifest.permission_group.STORAGE}, 123);
            }
        }
    }


@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 123){
            int grantResult = grantResults[0];
            if (grantResult == PackageManager.PERMISSION_GRANTED){
                start();
            } else if (grantResult == PackageManager.PERMISSION_DENIED 
                    && !shouldShowRequestPermissionRationale(Manifest.permission_group.STORAGE)){
                //权限被禁止了,引导用户到设置页
            }
        }
    }

It can be seen that in actual development, the role of shouldShowRequestPermissionRationale is only used to judge whether the permission is prohibited

Guess you like

Origin blog.csdn.net/qq_31031769/article/details/125647511