Android 8.0都出来了,可能有不少应用一直都还没有适配Android6.0。适配过的同学对以下几句代码都不陌生,每个需要权限的activity都需要这么一段代码:

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults){
if (requestCode == CODE_FOR_WRITE_PERMISSION){
if (permissions[0].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE)
&&grantResults[0] == PackageManager.PERMISSION_GRANTED){
// 用户同意
} else {
// 当用户禁止后,未勾选不再提醒时,以下接口返回true
if (shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
showPermissionDialog();return;
} else {
//用户禁止后,勾选了不再提醒,按照自己需求进行提示或引导
}
}
}
}

请求权限的时候,需要以下这段代码:

// 使用兼容库就无需判断系统版本
int hasWriteContactsPermission = ContextCompat.checkSelfPermission(getApplication(), Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (hasWriteContactsPermission == PackageManager.PERMISSION_GRANTED) {
} // 需要弹出dialog让用户手动赋予权限
else {
ActivityCompat.requestPermissions(Acivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, CODE_FOR_WRITE_PERMISSION);
}

于是想到实现一句代码就实现权限的适配,想到了在BaseActivity里实现,

//定义需要的成员变量和接口
private SparseArray<PermissionCallback> permissionCallbackList;
private ShopDialog permissionShopDialog = null;
public interface PermissionCallback {
void permissionGranted();
void noPermissionDenied();
String permissionDesc();
}

重写onRequestPermissionsResult

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (null == permissionCallbackList) {
return;
}
PermissionCallback permissionCallback = permissionCallbackList.get(requestCode);
if (null == permissionCallback) {
return;
}
if (grantResults.length == 0 || permissions.length == 0) {
return;
}
if (grantResults[0] == PackageManager.PERMISSION_GRANTED){
permissionCallback.permissionGranted();
permissionCallbackList.remove(requestCode);
} else {
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permissions[0])) {
if (TextUtils.isEmpty(permissionCallback.permissionDesc())) {
ActivityCompat.requestPermissions(activity, permissions, requestCode);
} else {
if (permissionShopDialog == null) {
permissionShopDialog = new ShopDialog();
}
permissionShopDialog.setCancelable(false);
permissionShopDialog.setContent(permissionCallback.permissionDesc())
.setRightText("确定")
.setRightClickListener(v -> {
permissionShopDialog.dismissAllowingStateLoss();
ActivityCompat.requestPermissions(activity, permissions, requestCode);
})
.show(activity);
}
} else {
if (!TextUtils.isEmpty(permissionCallback.permissionDesc())) {
ToastUtils.toast(permissionCallback.permissionDesc());
}
permissionCallback.noPermissionDenied();
permissionCallbackList.remove(requestCode);
}
}
}
public void requestPermission(String permission, PermissionCallback permissionCallback) {
if (null == permissionCallback) {
return;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(getApplication(), permission) == PackageManager.PERMISSION_GRANTED) {
permissionCallback.permissionGranted();
} else {
Calendar calendar = Calendar.getInstance();
int code = calendar.get(Calendar.MINUTE) * 60 + calendar.get(Calendar.SECOND);
if (null == permissionCallbackList) {
permissionCallbackList = new SparseArray<>();
}
permissionCallbackList.put(code, permissionCallback);
ActivityCompat.requestPermissions(activity, new String[]{permission}, code);
}
} else {
permissionCallback.permissionGranted();
}
}

调用的时候,只要一句代码即可:

//activity 需要继承于BaseActivity
activity.requestPermission(Manifest.permission.CAMERA, new BaseActivity.PermissionCallback() {
@Override
public void permissionGranted() {
//打开相机
}
@Override
public void noPermissionDenied() {
//被拒绝后,需要的操作
}
@Override
public String permissionDesc() {
return context.getString(R.string.camera_permission_denied);
}
});

标签: none

评论已关闭