请求运行时权限时选中“不再询问”框是否会禁用未来的对话框?

Does checking the Never ask again box when asking for a runtime permission disable future dialogs?

如果用户拒绝勾选不再询问 的运行时权限,这是否会禁用以后请求相同权限的能力?用户是否必须通过设置才能启用该权限?

在我的应用程序中,当我调用

ActivityCompat.requestPermissions(this, 
            new String[]{Manifest.permission.READ_PHONE_STATE}, 
            0)

并且该权限之前已被拒绝 不再询问,它根本不会显示对话框。这是预期的行为吗?

这是预期的行为。

来自the documentation

When the system asks the user to grant a permission, the user has the option of telling the system not to ask for that permission again. In that case, any time an app uses requestPermissions() to ask for that permission again, the system immediately denies the request. The system calls your onRequestPermissionsResult() callback method and passes PERMISSION_DENIED, the same way it would if the user had explicitly rejected your request again. This means that when you call requestPermissions(), you cannot assume that any direct interaction with the user has taken place.

是的,它将禁用。 但是,您可以做一些事情来检测用户是否设置了一些不再请求的权限。您可以在onRequestPermissionsResult()方法

中查看
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    for(String permission: permissions){
            if(ActivityCompat.shouldShowRequestPermissionRationale(this, permission)){
                //denied
                Log.e("denied", permission);
            }else{
                if(ActivityCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED){
                    //allowed
                    Log.e("allowed", permission);
                } else{
                    //set to never ask again
                    Log.e("set to never ask again", permission);
                    //do something here.
                }
            }
        }
}

我已经准备了一个帮助程序库,以允许在 GitHub 上处理这种情况 here

是的,James McCracken,这是预期的行为。仅供参考,如果用户选择了不再询问,则您无法打开该请求权限对话框。但是您可以向用户显示信息。

@Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case REQUEST_CODE:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(MainActivity.this, "Permission Granted", Toast.LENGTH_SHORT).show();
                    // do your work here
                } else if (Build.VERSION.SDK_INT >= 23 && !shouldShowRequestPermissionRationale(permissions[0])) {
                    Toast.makeText(MainActivity.this, "Go to Settings and Grant the permission to use this feature.", Toast.LENGTH_SHORT).show();
                   // User selected the Never Ask Again Option
                } else {
                    Toast.makeText(MainActivity.this, "Permission Denied", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    } 

如果您想在用户选择 "Never ask Again" 选项后打开设置屏幕,请使用以下代码

Intent i = new Intent();
i.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
i.addCategory(Intent.CATEGORY_DEFAULT);
i.setData(Uri.parse("package:" + context.getPackageName()));
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
i.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
context.startActivity(i);