shouldShowRequestPermissionRationale 未按预期工作 [英] shouldShowRequestPermissionRationale not working as expected

查看:37
本文介绍了shouldShowRequestPermissionRationale 未按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力检查并获得 API 级别 23 及以上用户的许可.android.com 说:

I'm working on checking and getting permission from user with API level 23 and above. So here is a confusing thing for me, android.com says:

shouldShowRequestPermissionRationale() 方法返回 true 如果应用程序之前已经请求过此权限并且用户拒绝了该请求.如果用户过去拒绝了权限请求并在权限请求系统对话框中选择了不再询问选项,则此方法返回 false

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

另一方面,它提供了以下代码来检查权限并在必要时请求权限

in other side it gives following code for checking permission and request permission if its neccessery

// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
            Manifest.permission.READ_CONTACTS)
    != PackageManager.PERMISSION_GRANTED) {

// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
        Manifest.permission.READ_CONTACTS)) {

    // Show an explanation to the user *asynchronously* -- don't block
    // this thread waiting for the user's response! After the user
    // sees the explanation, try again to request the permission.

} else {

    // No explanation needed, we can request the permission.

    ActivityCompat.requestPermissions(thisActivity,
            new String[]{Manifest.permission.READ_CONTACTS},
            MY_PERMISSIONS_REQUEST_READ_CONTACTS);

    // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
    // app-defined int constant. The callback method gets the
    // result of the request.
    }
}

上例中的

else 作用域在用户不允许时运行,并检查 Don't Ask Again,对吗?因此,使用此代码,用户在第一次运行时从未被要求获得许可.我测试了该代码,结果正是我所期望的.那么,如果用户之前拒绝了我的请求,我如何请求第一次运行的许可并执行某些操作,如果用户拒绝我的请求并检查不要再问,我如何执行某些操作?

else scope in above example runs if user doesn't allow permission and check Don't Ask Again, Right? So with this code user never being asked for permission at first time run. I tested that code and the result is what I expected. So How could I request permission for first time run and do something if user previously denied my request and do something if user deny my request and check Don't Ask Again?

推荐答案

您可以执行以下操作.

  1. 每次仅通过检查尚未授予权限来询问权限.不要处理这个方法中的不要再问".(如果用户勾选Don't ask again",操作系统会自动处理,不会再请求许可,并会回电拒绝许可)
  2. 处理回调方法 onRequestPermissionsResult() 中的 ActivityCompat.shouldShowRequestPermissionRationale().
  1. Ask permission every time by only checking that permission is not granted already. Don't handle the "Don't ask again" in this method. (if user checks "Don't ask again" the OS will automatically handle and will not ask for permission again and will give a call back of permission denied)
  2. Handle the ActivityCompat.shouldShowRequestPermissionRationale() in the call back method onRequestPermissionsResult().

这是我每次都请求许可的代码

Here is my code in which I am asking for permission everytime

public void checkAndAskCameraPermission() {
        if (ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {

            ActivityCompat.requestPermissions(context,
                    new String[]{Manifest.permission.CAMERA}, CAMERA_PERMISSION_REQUEST_ID);
        }
    }

这就是我处理回电的方式

This is how I handles the call back

@Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        switch (requestCode) {
            case CAMERA_PERMISSION_REQUEST_ID: {
                if (!ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {

                  // Do something if permission is not granted and the user has also checked the **"Don't ask again"**
                } else if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_DENIED) {
                    // Do something if permission not granted
                }
            }
        }
    }

这篇关于shouldShowRequestPermissionRationale 未按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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