最近的项目将权限申请写在欢迎页面,但是出现了个bug严重影响用户体验:点击第一个权限同意后第二个会自动弹出权限被拒绝后才弹出的权限打开提示框,如图所示:
经过log跟踪后发现问题原因:由于权限弹出框操作以后,该界面会先调用onResume()方法,然后再执行
onRequestPermissioinResult()方法,而我的权限申请都是线性的,只有上一步同意后才会弹出下一步的权限申请提示框,这就造成如果拒绝中间的某个权限,后面界面就无法申请了,必须跳转到系统设置中打开权限,但是返回后界面空白,没有继续弹出权限申请提示框;所以我在onResume()方法中放入第一个权限的判断方法,希望在设置中打开权限返回后某一个权限返回后会重走一遍,好让用户继续申请;于是上面的问题就出现了,于是继续修改,bug解决,不多说了上代码
@Override
protected void onResume() {
super.onResume();
switch (step){
case 0:
if(!isRequest1){
MyLog.e(TAG,"here is onResume1");
initPermissions();
} else {
isRequest1 = false;
}
break;
case 1:
if(!isRequest2){
MyLog.e(TAG,"here is onResume2");
initGPSPermissions();
} else {
isRequest2 = false;
}
break;
case 2:
if(!isRequest3){
MyLog.e(TAG,"here is onResume3");
initCameraPermissions();
} else {
isRequest3 = false;
}
break;
case 3:
if(!isRequest4){
MyLog.e(TAG,"here is onResume4");
initPhoneInfoPermissions();
} else {
isRequest4 = false;
}
break;
case 4:
goNext();
break;
}
}
上面是onResume()方法
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
if (requestCode == PERMISSIONS_REQUEST_READ_CONTACTS) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.e(TAG, "同意授权---1111");
step = 1;
// initGPSPermissions();
} else {
isRequest1 = true;
Log.e(TAG, "拒绝授权---1111");
// Toast.makeText(this, "请在 设置-应用管理 中开启此应用的存储授权。", Toast.LENGTH_SHORT).show();
goOpenPremission("存储");
}
} else if (requestCode == PERMISSIONS_REQUEST_GPS) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.e(TAG, "同意授权---2222");
step = 2;
// initCameraPermissions();
} else {
Log.e(TAG, "拒绝授权---2222");
isRequest2 = true;
// Toast.makeText(this, "请在 设置-应用管理 中开启此应用的定位授权。", Toast.LENGTH_SHORT).show();
goOpenPremission("定位");
}
}else if (requestCode == PERMISSIONS_REQUEST_CAMERA) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.e(TAG, "同意授权---3333");
step = 3;
// initPhoneInfoPermissions();
} else {
Log.e(TAG, "拒绝授权---3333");
isRequest3 = true;
// Toast.makeText(this, "请在 设置-应用管理 中开启此应用的定位授权。", Toast.LENGTH_SHORT).show();
goOpenPremission("拍照");
}
}else if (requestCode == PERMISSIONS_REQUEST_PHONE_INFO) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.e(TAG, "同意授权---4444");
step = 4;
// goNext();
} else {
isRequest4 = true;
Log.e(TAG, "拒绝授权---4444");
// Toast.makeText(this, "请在 设置-应用管理 中开启此应用的定位授权。", Toast.LENGTH_SHORT).show();
goOpenPremission("读取手机信息");
}
}
}
// 授权管理
private void initPermissions() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
Log.i(TAG, "需要授权 ");
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
Log.i(TAG, "拒绝过了");
goOpenPremission("存储");
// isRequest = true;
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSIONS_REQUEST_READ_CONTACTS);
} else {
Log.i(TAG, "进行授权");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSIONS_REQUEST_READ_CONTACTS);
}
} else {
// Log.i(TAG, "不需要授权 ");
step = 1;
initGPSPermissions();
}
}
// 授权管理
private void initGPSPermissions() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) {
// isRequest = true;
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSIONS_REQUEST_GPS);
// goOpenPremission("定位");
} else {
Log.i(TAG, "进行授权");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSIONS_REQUEST_GPS);
}
} else {
// Log.i(TAG, "不需要授权 ");
step = 2;
initCameraPermissions();
}
}
// 授权管理
private void initCameraPermissions() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
// isRequest = true;
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, PERMISSIONS_REQUEST_CAMERA);
// goOpenPremission("相机");
} else {
Log.i(TAG, "进行授权");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, PERMISSIONS_REQUEST_CAMERA);
}
} else {
// Log.i(TAG, "不需要授权 ");
step = 3;
initPhoneInfoPermissions();
}
}
// 授权管理
private void initPhoneInfoPermissions() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE)!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_PHONE_STATE)) {
// isRequest = true;
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, PERMISSIONS_REQUEST_PHONE_INFO);
// goOpenPremission("读取手机信息");
} else {
Log.i(TAG, "进行授权");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, PERMISSIONS_REQUEST_PHONE_INFO);
}
} else {
// Log.i(TAG, "不需要授权 ");
step = 4;
goNext();
}
}
当然,现在github上有很多权限管理的插件要更好用点,这个是直接复制了老项目的代码进行开发发现的遗留问题,记录一下。