开篇
android 6.0+要求我们动态请求权限。网上针对于请求权限的封装很多,你是否用着还爽呢?不管如何,你且尝试下我给你写的组件——CustomPermissionChecker。(不喜勿喷,谢谢哈!)
屏幕截图
立即体验
扫描以下二维码下载体验App(从0.2.3
版本开始,体验App内嵌版本更新检测功能):
JSCKit库传送门:https://github.com/JustinRoom/JSCKit
源码简析
1、CustomPermissionChecker.java:
就两个主要的方法:
-
public boolean checkPermissions(Activity activity, @IntRange(from = 0) int requestCode, OnCheckListener onCheckListener, String... permissions)
用途:请求权限。(请参阅方法里的注释)
public boolean checkPermissions(Activity activity, @IntRange(from = 0) int requestCode, OnCheckListener onCheckListener, String... permissions) {
if (permissions == null || permissions.length == 0)
throw new NullPointerException("The parameter permissions can't be null.");
this.activity = activity;
this.onCheckListener = onCheckListener;
this.permissions = Arrays.asList(permissions);
boolean isAllGranted = true;
List<String> unGrantedPermissions = new ArrayList<>();
for (String permission : permissions) {
boolean isGranted = ActivityCompat.checkSelfPermission(activity.getApplicationContext(), permission) == PackageManager.PERMISSION_GRANTED;
isAllGranted = isAllGranted && isGranted;
if (!isGranted)
unGrantedPermissions.add(permission);
}
//所请求的permissions都已授权通过
if (isAllGranted) {
if (onCheckListener != null)
onCheckListener.onResult(
requestCode,
true,
this.permissions,
null,
null
);
return true;
}
//请求授权
ActivityCompat.requestPermissions(activity, unGrantedPermissions.toArray(new String[unGrantedPermissions.size()]), 0);
return false;
}
-
public void onPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
用途:处理权限请求结果。(请参阅方法里的注释)
public void onPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (onCheckListener == null)
return;
List<String> deniedPermissions = new ArrayList<>();
List<String> shouldShowPermissions = new ArrayList<>();
for (int i = 0; i < grantResults.length; i++) {
String permission = permissions[i];
if (grantResults[i] == PackageManager.PERMISSION_DENIED){
deniedPermissions.add(permissions[i]);
boolean shouldShow = ActivityCompat.shouldShowRequestPermissionRationale(activity, permission);
if (!shouldShow)
shouldShowPermissions.add(permission);
}
}
//移除掉已拒绝的之后就是授权通过的
this.permissions.removeAll(deniedPermissions);
onCheckListener.onResult(
requestCode,
deniedPermissions.size() == 0,
this.permissions,
deniedPermissions,
shouldShowPermissions
);
//处理收尾工作
onCheckListener.onFinally(requestCode);
}
2、CustomPermissionChecker定义好了,接下来我们把它封装到框架里面去,复写一个abstract
的 activity
,其他的Activity继承它:
public abstract class APermissionCheckActivity extends AppCompatActivity {
private CustomPermissionChecker customPermissionChecker = null;
public final boolean checkPermissions(String... permissions) {
return checkPermissions(0, null, permissions);
}
public final boolean checkPermissions(@IntRange(from = 0) int requestCode, CustomPermissionChecker.OnCheckListener checkListener, String... permissions) {
if (customPermissionChecker == null) {
customPermissionChecker = new CustomPermissionChecker();
}
return customPermissionChecker.checkPermissions(this, requestCode, checkListener, permissions);
}
/**
* Release resource. I suggest that you should call this method in {@link CustomPermissionChecker.OnCheckListener#onFinally(int)}.
*/
public final void recyclePermissionChecker() {
if (customPermissionChecker == null)
return;
customPermissionChecker = null;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
customPermissionChecker.onPermissionsResult(requestCode, permissions, grantResults);
}
}
公共方法
用法
不是我吹,用起来真是简单:
public void widgetClick(View v) {
//请求相机权限、SD卡权限
//注意:requestCode >= 0
checkPermissions(0x100, new CustomPermissionChecker.OnCheckListener() {
@Override
public void onResult(int requestCode, boolean isAllGranted, @NonNull List<String> grantedPermissions, @Nullable List<String> deniedPermissions, @Nullable List<String> shouldShowPermissions) {
if (isAllGranted){
toScannerActivity();
return;
}
if (shouldShowPermissions != null && shouldShowPermissions.size() > 0){
String message = "当前应用需要以下权限:\n\n" + getAllPermissionDes(shouldShowPermissions);
showPermissionRationaleDialog("温馨提示", message, "设置", "知道了");
}
}
@Override
public void onFinally(int requestCode) {
removePermissionChecker();
}
}, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA);
}
篇尾
就是这么简单!如果您觉得还不错的话,给个star(或加个关注)啰!我是JustinEoy,QQ:1006368252。
所有伟大的事迹和伟大的思想都有荒谬的开头。 —— 加缪
网友评论