我最近开始开发android 6.0 - 运行时权限请求。
我想知道调用shouldShowRequestPermissionRationale()
方法的次数。
这是我的代码:
@Override
protected void onResume() {
super.onResume();
if ((ContextCompat.checkSelfPermission(LaunchActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED)){
dbOperation();
if (NetworkUtil.isServiceReachable(this)){
checkAppVersion();
} else {
startApplication();
}
} else {
if (!ActivityCompat.shouldShowRequestPermissionRationale(LaunchActivity.this,Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
showMessageOKCancel("You need to allow access to Storage/Media for storing your Records.",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(LaunchActivity.this,new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE},
PermissionConstants.WRITE_EXTERNAL_STORAGE_PERMISSION);
}
});
return;
}
ActivityCompat.requestPermissions(LaunchActivity.this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},PermissionConstants.WRITE_EXTERNAL_STORAGE_PERMISSION);
return;
}
}
在回调方法中,我这样做:
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
// super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode){
case PermissionConstants.WRITE_EXTERNAL_STORAGE_PERMISSION:{
if (grantResults.length>0 && grantResults[0]== PackageManager.PERMISSION_GRANTED){
dbOperation();
// Permission is granted Read and Write external storage.
if (NetworkUtil.isServiceReachable(this)){
checkAppVersion();
} else {
startApplication();
}
}else {
if (!ActivityCompat.shouldShowRequestPermissionRationale(LaunchActivity.this,Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
showMessageOKCancel("You need to allow access to Storage/Media for storing your Records.",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(LaunchActivity.this,new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE},
PermissionConstants.WRITE_EXTERNAL_STORAGE_PERMISSION);
}
});
return;
}else {
ActivityCompat.requestPermissions(LaunchActivity.this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},PermissionConstants.WRITE_EXTERNAL_STORAGE_PERMISSION);
}
//break;
}
}
break;
default:super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
在这种情况下。当用户拒绝权限而不选择don.t再次询问时,android会显示2次请求权限对话框,然后进入无限循环。
我想知道如果用户多次拒绝请求但未选中don't ask again
复选框会发生什么?
如果用户选中don't ask again
复选框并拒绝请求会怎样?
在这种情况下是否会调用shouldShowRequestPermissionRationale()
方法?
如果是,多少次?
如果否为什么不呢?
任何帮助将不胜感激。
答案 0 :(得分:0)
如果用户多次拒绝请求而不选择不再询问复选框,则对于shouldShowRequestPermissionRationale(),您将始终为true。因此,开发人员决定在显示Rationale对话框后第二次显示权限请求对话框时,让用户使用Rationale对话框和权限请求对话框循环,或将其视为拒绝。
如果用户选中“不再询问”复选框并拒绝该请求,则对于shouldShowRequestPermissionRationale(),即用户拒绝该权限,您将收到错误。
所以建议的流程应该是: -