我正在检查并获得API级别为23及以上的用户的许可。因此,这里是一个令人困惑的事情对我来说,android.com说:如果应用程序已请求此权限以前与用户拒绝了这一要求shouldShowRequestPermissionRationale未按预期工作
shouldShowRequestPermissionRationale()方法返回true。 如果用户拒绝了在过去的许可请求,并选择在许可请求的系统对话框中不要再次询问选项,这种方法在另一边返回false
它给检查的权限,并要求下面的代码如果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
范围在上面的例子中运行的权限,如果用户不允许许可和检查不要再次询问,对不对?因此,在第一次运行时,这个代码用户永远不会被要求获得许可。我测试了这个代码,结果就是我所期望的。 那么我怎么能要求第一次运行的权限,如果用户先前拒绝了我的请求并做了一些事情,如果用户拒绝我的请求并检查不要再提问?
_“因此,与从来没有被要求在第一次运行权限验证码用户” _'shouldShowRequestPermissionRationale'应该返回'假'如果你的应用程序尚未向用户请求权限,那么'else'子句将在第一次运行时执行。 – Michael
[Android M的可能重复权限:对shouldShowRequestPermissionRationale()函数的使用感到困惑(https://stackoverflow.com/questions/32347532/android-m-permissions-confused-on-the-usage-of-shouldshowrequestpermissionrati) –
@Michael但android.com认为是不同的:如果用户过去关闭了权限请求,并在权限请求系统对话框中选择了“不要再请求”选项,该方法返回false。 –