开篇
android 6.0+要求我们动态请求权限。网上针对于请求权限的封装很多,你是否用着还爽呢?不管如何,你且尝试下我给你写的组件——PermissionChecker。(不喜勿喷,谢谢哈!)
屏幕截图
立即体验
扫描以下二维码下载体验App(从0.2.3
版本开始,体验App内嵌版本更新检测功能):
JSCKit库传送门:https://github.com/JustinRoom/JSCKit
源码简析
PermissionChecker.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 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 deniedPermissions = new ArrayList<>();
List 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、定义好之后,接下来我们把它封装到框架里面去,复写一个abstract
的 activity
,其他的Activity继承它:
public abstract class BaseAppCompatActivity extends AppCompatActivity {
public PermissionChecker permissionChecker;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initComponent();
...
}
/**
* Initialize components here.
*/
@CallSuper
public void initComponent() {
permissionChecker = new PermissionChecker();
...
}
/**
* Destroy components here.
*/
@CallSuper
public void destroyComponent() {
permissionChecker = null;
...
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
permissionChecker.onPermissionsResult(requestCode, permissions, grantResults);
}
@Override
protected void onDestroy() {
destroyComponent();
super.onDestroy();
}
}
用法
不是我吹,用起来真是简单:
public void widgetClick(View v) {
//请求相机权限、SD卡权限
//注意:requestCode >= 0
permissionChecker.checkPermissions(0x100, new PermissionChecker.OnCheckListener() {
@Override
public void onResult(int requestCode, boolean isAllGranted, @NonNull List grantedPermissions, @Nullable List deniedPermissions, @Nullable List shouldShowPermissions) {
if (isAllGranted){
toScannerActivity();
return;
}
if (shouldShowPermissions != null && shouldShowPermissions.size() > 0){
String message = "当前应用需要以下权限:\n\n" + getAllPermissionDes(shouldShowPermissions);
showPermissionRationaleDialog("温馨提示", message, "设置", "知道了");
}
}
}, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA);
}
篇尾
就是这么简单!如果您觉得还不错的话,给个star(或加个关注)啰!QQ:1006368252
。
所有伟大的事迹和伟大的思想都有荒谬的开头。 —— 加缪