Android M 权限:对 shouldShowRequestPermissionRationale() 函数的使用感到困惑

问题描述:

我正在阅读有关 Android M 中新权限模型的官方文档.它讨论了 shouldShowRequestPermissionRationale() 函数,如果应用程序请求此函数,该函数返回 true之前的权限并且用户拒绝了该请求.如果用户过去拒绝了权限请求并选择了不再询问选项,则此方法返回 false.

I was going through the official doc about the new Permissions model in Android M. It talks about the shouldShowRequestPermissionRationale() function which 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, this method returns false.

但是我们如何区分以下两种情况?

But how can we differentiate between the following two cases?

案例 1:该应用没有权限,并且之前未向用户请求权限.在这种情况下,shouldShowRequestPermissionRationale() 将返回 false,因为这是我们第一次询问用户.

Case 1: The app doesn't have a permission and the user has not been asked for the permission before. In this case, shouldShowRequestPermissionRationale() will return false because this is the first time we're asking the user.

情况 2:用户拒绝了权限并选择了不再询问",在这种情况下,shouldShowRequestPermissionRationale() 也会返回 false.

Case 2: The user has denied the permission and selected "Don't ask again", in this case too shouldShowRequestPermissionRationale() will return false.

我想在案例 2 中将用户发送到应用程序的设置页面.我如何区分这两种情况?

I would want to send the user to the App's settings page in Case 2. How do i go about differentiating these two cases?

在M Preview 1之后,如果对话框第一次显示,则没有不再询问em> 复选框.

After M Preview 1, if the dialog is displayed for the first time, there is no Never ask again checkbox.

如果用户拒绝权限请求,权限对话框中将出现一个不再询问复选框第二次请求权限.

If the user denies the permission request, there will be a Never ask again checkbox in the permission dialog the second time permission is requested.

所以逻辑应该是这样的:

So the logic should be like this:

  1. 请求权限:

  1. Request permission:

if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(context, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE);
} else {
    //Do the stuff that requires permission...
}

  • 检查权限是否在 onRequestPermissionsResult 中被拒绝或授予.

    如果权限之前被拒绝,这次将在权限对话框中出现一个不再询问复选框.

    If the permission was denied previously, this time there will be a Never ask again checkbox in the permission dialog.

    调用 shouldShowRequestPermissionRationale 以查看用户是否选中了不再询问.shouldShowRequestPermissionRationale 方法仅在用户选择不再询问或设备策略禁止应用程序拥有该权限时返回 false:

    Call shouldShowRequestPermissionRationale to see if the user checked Never ask again. shouldShowRequestPermissionRationale method returns false only if the user selected Never ask again or device policy prohibits the app from having that permission:

    if (grantResults.length > 0){
        if(grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            //Do the stuff that requires permission...
        }else if (grantResults[0] == PackageManager.PERMISSION_DENIED){
            // Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(context, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                //Show permission explanation dialog...
            }else{
                //Never ask again selected, or device policy prohibits the app from having that permission.
                //So, disable that feature, or fall back to another situation...
            }
        }
    }
    

  • 因此,您无需跟踪用户是否选中不再询问.

    So, you won't have to track if a user checked Never ask again or not.