shouldShowRequestPermissionRationale无法正常工作

Art*_*tin 5 android android-permissions

我正在检查API级别23及以上的用户并获得其许可。所以这对我来说是一件令人困惑的事情,android.com说:

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

另一方面,它提供以下代码来检查许可权和请求许可权(如果需要)

// 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.
    }
}
Run Code Online (Sandbox Code Playgroud)

else如果用户不允许权限并检查“ 不要再询问”,对吗?因此,有了此代码,用户不会在第一次运行时被要求获得许可。我测试了该代码,结果是我所期望的。那么,如果用户先前拒绝了我的请求,并且我拒绝了我的请求并选中了“不再询问”,那么我如何才能首次请求运行许可?

sal*_*man 2

首先检查是否授予了权限,然后执行您需要的操作,否则如果权限被拒绝,则检查您是否应该通过 shouldShowRequestPermissionRationale 请求所需的权限。
如果 shouldShowRequestPermissionRationale 返回 false 那么您可以向用户显示一条消息,告诉他们手动启用权限。
否则如果shouldShowRequestPermissionRationale返回true则请求权限并在onRequestPermissionsResult中获取授予权限的结果

update ,这只是一个简单的代码来显示步骤:

if (checkSelfPermission == granted){
 // do what you want 
 } else if (shouldShowRequestPermissionRationale == true) {
        // you can request the permission you want from the user, then check whether permission granted or not inside onRequestPermissionsResult method
 } else { 
        // here you know the permission is not granted , and also the user check on "Dont ask Again!" checkbox, so all you can to do is just tell the user to enable the permission manually from app permissions through Toast message or any thing else
        }
Run Code Online (Sandbox Code Playgroud)