Android M权限:对shouldShowRequestPermissionRationale()函数的使用感到困惑 [英] Android M Permissions : Confused on the usage of shouldShowRequestPermissionRationale() function

查看:864
本文介绍了Android M权限:对shouldShowRequestPermissionRationale()函数的使用感到困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看有关Android M中新Permissions模型的官方文档.它讨论了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 :用户已拒绝许可权,并选择不再询问",在这种情况下,ShowShowPermissionPermissionRationale()也会返回false.

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

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

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预览1之后,如果第一次显示该对话框 ,则没有再也不会询问复选框.

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中被拒绝或授予.

  • Check if the permission was denied or granted in 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.

    这篇关于Android M权限:对shouldShowRequestPermissionRationale()函数的使用感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆