The project needs to apply for read and write storage permissions, but dynamic permissions are always not available. Solution:
First judge whether the permission is obtained, here use PermissionChecker, because some domestic models always return 0 through ContextCompat
//判断是否有指定权限
public boolean isPermission(Context context, String[] perValue) {// 判断手机版本,如果低于6.0 则不用申请权限,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//7.0及以上
for (int i = 0; i < perValue.length; i++) {
if ((PermissionChecker.checkSelfPermission(context, perValue[i]) != PackageManager.PERMISSION_GRANTED)) {
return false;
}
}
}
return true;
}
Since the permission inquiry window has not been able to pop up and the reason cannot be found, I want to jump directly to the permission setting interface and let the user do it himself.
//检查是否允许允许重新检查
public void checkPermission(){
if (isPermission(context, needPermissions)) {//权限全部通过
beforeLogin();
} else {
// showTakePermission(this, context);
ToastUtil.showToast("请允许'存储'权限!重新启动",R.drawable.ic_error_outline_black_24dp);
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivityForResult(intent,1);
}
You can recheck the logic behind in onActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==1) {
//返回逻辑
}
}
Personal development notes There are good solutions and welcome guidance