Android shouldShowRequestPermissionRationale 有bug?

fas*_*goc 4 android android-permissions

我认为有一个错误 shouldShowRequestPermissionRationale

代码是...

@Override
protected void onResume() {
    super.onResume();
    if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_DENIED &&
            ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_DENIED) {
        if(ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_COARSE_LOCATION) ||
                ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) {
            new AlertDialog.Builder(this)....show();
    } else {
        // do something...
}
Run Code Online (Sandbox Code Playgroud)

首先安装了应用程序,我们不允许该权限。所以当 onResume 被调用时,AlertDialog 应该出现。但是没有出现...

如果我们进入应用程序的设置,并允许权限。所以我们播放应用程序代码( // do something)。再次,我们进入应用程序的设置,拒绝权限。然后我们重新启动应用程序,就会出现 AlertDialog。

为什么应用程序会这样运行?

Ash*_*kla 5

来自开发者文档:

shouldShowRequestPermissionRationale()

如果应用程序先前已请求此权限并且用户拒绝了该请求,则此方法返回 true。

注意:如果用户过去拒绝了权限请求并在权限请求系统对话框中选择了不再询问选项,则此方法返回 false。

问题是您在使用之前没有请求权限

 ActivityCompat.requestPermissions();
Run Code Online (Sandbox Code Playgroud)

因此它没有显示对话框。

当您从“设置”手动授予权限或拒绝权限时,它假定您拒绝了该权限,这就是它显示警报对话框的原因。