下面是我的代码,我调用此代码来获取运行时权限。在这种情况下,"shouldshowrequestpermissionrationale总是返回false“。我找不到一个解决方案,为什么它会变成这样。因此,不显示运行时权限警报。请给我一个解决方案。
private void checkRuntimePermission() {
Logger.infoLog("checkRuntimePermission");
if(ActivityCompat.checkSelfPermission(this, permissionsRequired[0]) != PackageManager.PERMISSION_GRANTED){
Logger.infoLog("checkRuntimePermission first if");
if(ActivityCompat.shouldShowRequestPermissionRationale(WelcomeActivity.this,permissionsRequired[0])){
Logger.infoLog("checkRuntimePermission if");
//just request the permission
//Show Information about why you need the permission
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Need Multiple Permissions");
builder.setMessage("This app needs Camera and Location permissions.");
builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
ActivityCompat.requestPermissions(WelcomeActivity.this,permissionsRequired,PERMISSION_CALLBACK_CONSTANT);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}else{
Logger.infoLog("Show request permission rationale false");
}
} else {
//You already have the permission, just go ahead.
Logger.infoLog("Permission given already");
proceedAfterPermission();
}
}发布于 2017-11-27 10:15:39
在google文档中:
“如果应用程序以前请求过此权限,但用户拒绝了该请求,则此方法返回true。”
所以,你应该先调用requestPermissions(...),然后再使用shouldShowRequestPermissionRationale(...)可以得到你想要的结果。
最好的方法是在onRequestPermissionsResult(...)中始终使用requestPermissions(...)
发布于 2017-08-15 08:45:42
您正在调用的方法提出了这样的问题:“我们是否应该显示请求此权限的原因?”
从Docs "This method returns true if the app has requested this permission previously and the user denied the request." https://developer.android.com/training/permissions/requesting.html
如果该值为false,您仍然希望请求权限,但不需要显示警告对话框。因此,在else块中,只需简单地
ActivityCompat.requestPermissions(WelcomeActivity.this,permissionsRequired,PERMISSION_CALLBACK_CONSTANT);https://stackoverflow.com/questions/45684842
复制相似问题