6.0 刚出来,曾简单封装过一个工具类,今天,再完善了,一下,踢出了不要的代码;
https://github.com/zhaoyubetter/PermissionCheck
使用方法
- 在使用初检测:
findViewById(R.id.requestOne).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PermissionUtils.checkPermissions(MainActivity.this,
new String[]{Manifest.permission.CAMERA}, "照相机", new Runnable() {
@Override
public void run() {
// 执行操作
}
});
}
});
- onRequestPermissionsResult方法的复写:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
PermissionUtils.requestResult(requestCode, permissions, grantResults, new Runnable() {
@Override
public void run() {
// 权限获取成功执行的代码
}
}, new Runnable() {
@Override
public void run() {
// 权限获取失败执行的代码
}
});
}
工具类代码如下:
public final class PermissionUtils {
public static final int REQUEST_CODE = 20;
/**
* 请求权限
*
* @param obj
* @param reqPermissions 权限列表
* @param explain 申请权限解释说明
* @param runnable 有权限时,执行的runnable
*/
public static void checkPermissions(final Object obj, final String[] reqPermissions, final String explain, final Runnable runnable) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// 检测类型
Activity activity = getActivity(obj);
if (activity == null || reqPermissions == null) {
return;
}
// 需要申请的权限
final List<String> needReqList = new ArrayList<>();
// 一次申请多个权限
boolean needShowExplain = false;
for (int i = 0; i < reqPermissions.length; i++) {
needShowExplain = addPermission(obj, needReqList, reqPermissions[i]);
}
// 有权限需要申请
if (needReqList.size() > 0 && !activity.isFinishing()) {
if (!TextUtils.isEmpty(explain) && needShowExplain) {
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setMessage(explain);
builder.setCancelable(false);
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
request(obj, needReqList);
}
});
builder.create().show();
} else {
request(obj, needReqList);
}
} else {
if (runnable != null) {
runnable.run();
}
}
} else {
if (runnable != null) {
runnable.run();
}
}
}
/**
* 权限请求结果
* onRequestPermissionsResult 调用
*
* @param requestCode
* @param permissions
* @param grantResults
* @param successRunnable 权限获取成功,执行的任务
* @param failRunnable 权限获取失败,执行的任务
*/
public static void requestResult(int requestCode, String[] permissions, int[] grantResults, final Runnable successRunnable, final Runnable failRunnable) {
if (requestCode == REQUEST_CODE) {
boolean isGranted = true;
int size = permissions.length;
for (int i = 0; i < size; i++) {
if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
isGranted = false;
break;
}
}
if (isGranted && successRunnable != null) {
successRunnable.run(); // 执行任务
} else if (!isGranted && failRunnable != null) { // 权限被拒绝
failRunnable.run(); // 执行任务
}
}
}
/**
* 请求权限
*
* @param object
* @param permissionsList
*/
private static void request(final Object object, List<String> permissionsList) {
if (object instanceof Activity) {
ActivityCompat.requestPermissions((Activity) object, permissionsList.toArray(new String[permissionsList.size()]), REQUEST_CODE);
} else if (object instanceof Fragment) {
((Fragment) object).requestPermissions(permissionsList.toArray(new String[permissionsList.size()]), REQUEST_CODE);
}
}
/**
* 添加要求权限列表
*
* @param obj
* @param permissionsList
* @param permission
* @return true 表示需要解释,false不需要
*/
private static boolean addPermission(final Object obj, List<String> permissionsList, String permission) {
// 检测类型
Activity activity = getActivity(obj);
if (activity == null) {
return true;
}
if (ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) {
permissionsList.add(permission);
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)) {
return true;
}
}
return false;
}
/**
* 获取权限请求的acitivity
*
* @param obj
* @return
*/
private static Activity getActivity(final Object obj) {
Activity activity = null;
if (obj instanceof Activity) {
activity = (Activity) obj;
} else if (obj instanceof Fragment) {
Fragment fragment = (Fragment) obj;
activity = fragment.isAdded() ? fragment.getActivity() : null;
} else if (obj instanceof android.app.Fragment) {
Fragment fragment = (Fragment) obj;
activity = fragment.isAdded() ? fragment.getActivity() : null;
} else {
throw new IllegalArgumentException("不支持的参数类型:" + obj.getClass());
}
return activity;
}
}