前言
现在来谈 Android 6.0 运行时权限适配,可以说是很过时了,可是为什么还要写呢?
一是试用了目前 GitHub 上排名比较靠前的开源项目,确实都很棒,但是在易用性还是难以令人满意,便萌生了自己撸一个的想法。
二是看了下目前国内主流的应用,发现很多都还没有适配 Android 6.0 ,因此觉得这篇文章还有它的意义。
使用
既然上面说到了易用性,那我们先来看看使用方法
在需要申请权限的地方调用
PermissionReq.with(this) // Activity or Fragment
.permissions(Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE) // 需要申请的权限
.result(new PermissionReq.Result() { // 申请结果回调
@Override
public void onGranted() { // 申请成功
// do something
}
@Override
public void onDenied() { // 申请失败
// do something
}
})
.request();
在 Activity 基类和 Fragment 基类中添加以下代码
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
PermissionReq.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
API 设计采用了比较流行的流式调用,不知道大家看了是什么感觉,我觉得使用起来比较简单,而且不会破坏原来的代码结构。
说到这里,我多说一句,现在有很多开源框架都是使用注解的方式来回调申请结果的,我觉得用这种方式虽然代码层次变少了,但是可读性变差了,而且可能会破坏原来代码结构。
这里还有个亮点不知道大家注意到没,我们没有用到 RequestCode
,那 RequestCode
哪里去了呢,在接下来的内容中我会告诉大家。
这也是我最喜欢的地方,不需要在每个申请权限的地方定义一个 RequestCode
,更不用担心 RequestCode
会重复,因为 PermissionReq
已经帮大家处理好了。
源码解析
看完使用方式后我们来看下内部实现,我们按照流程来看
首先我们要检查 App 注册了哪些权限,如果要申请的权限压根就没有在 Manifest 中注册,那么肯定会失败的
initManifestPermission(activity);
for (String permission : mPermissions) {
if (!sManifestPermissionSet.contains(permission)) {
if (mResult != null) {
mResult.onDenied();
}
return;
}
}
private static Set<String> sManifestPermissionSet;
private static synchronized void initManifestPermission(Context context) {
if (sManifestPermissionSet == null) {
sManifestPermissionSet = new HashSet<>();
try {
PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_PERMISSIONS);
String[] permissions = packageInfo.requestedPermissions;
Collections.addAll(sManifestPermissionSet, permissions);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
}
为了线程安全我们添加了 synchronized
修饰符。
如果要申请的权限已经在 Manifest 中注册了,我们接下来就要区分下系统版本了,如果系统版本低于 26 直接返回成功,否则才需要申请权限
这段代码比较简单,我就不贴了
如果系统版本 >= 26 ,那么才开始我们真正的申请流程
检查要申请的权限是否已经被允许,如果已经被允许,那么就没必要再申请了
List<String> deniedPermissionList = getDeniedPermissions(activity, mPermissions);
if (deniedPermissionList.isEmpty()) {
if (mResult != null) {
mResult.onGranted();
}
return;
}
private static List<String> getDeniedPermissions(Context context, String[] permissions) {
List<String> deniedPermissionList = new ArrayList<>();
for (String permission : permissions) {
if (ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
deniedPermissionList.add(permission);
}
}
return deniedPermissionList;
}
如果要申请的权限没有全部被允许,那么我们就需要向系统发送申请了
生成 RequestCode
int requestCode = genRequestCode();
private static AtomicInteger sRequestCode = new AtomicInteger(0);
private static int genRequestCode() {
return sRequestCode.incrementAndGet();
}
还记得上面说我们在使用时不需要定义 RequestCode 吗,至此,RequestCode 终于浮出水面
我们在内部使用一个静态自增的 AtomicInteger 作为 RequestCode ,保证 RequestCode 不会重复,使用 AtomicInteger 而不直接使用 int 是为了线程安全
向系统发送申请
String[] deniedPermissions = deniedPermissionList.toArray(new String[deniedPermissionList.size()]);
requestPermissions(mObject, deniedPermissions, requestCode);
sResultArray.put(requestCode, mResult);
@TargetApi(Build.VERSION_CODES.M)
private static void requestPermissions(Object object, String[] permissions, int requestCode) {
if (object instanceof Activity) {
((Activity) object).requestPermissions(permissions, requestCode);
} else if (object instanceof Fragment) {
((Fragment) object).requestPermissions(permissions, requestCode);
}
}
申请时区分来源,申请后把 Result 放入 Array 中保存起来,等待申请结果到达
申请结果到达后通知申请者
public static void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
Result result = sResultArray.get(requestCode);
if (result == null) {
return;
}
sResultArray.remove(requestCode);
for (int grantResult : grantResults) {
if (grantResult != PackageManager.PERMISSION_GRANTED) {
result.onDenied();
return;
}
}
result.onGranted();
}
到这里我们的申请权限流程已经走完了,源码也看完了。
完整代码
为了方便大家使用,我贴一下完整代码
public class PermissionReq {
private static AtomicInteger sRequestCode = new AtomicInteger(0);
private static SparseArray<Result> sResultArray = new SparseArray<>();
private static Set<String> sManifestPermissionSet;
public interface Result {
void onGranted();
void onDenied();
}
private Object mObject;
private String[] mPermissions;
private Result mResult;
private PermissionReq(Object object) {
mObject = object;
}
public static PermissionReq with(@NonNull Activity activity) {
return new PermissionReq(activity);
}
public static PermissionReq with(@NonNull Fragment fragment) {
return new PermissionReq(fragment);
}
public PermissionReq permissions(@NonNull String... permissions) {
mPermissions = permissions;
return this;
}
public PermissionReq result(@Nullable Result result) {
mResult = result;
return this;
}
public void request() {
Activity activity = getActivity(mObject);
if (activity == null) {
throw new IllegalArgumentException(mObject.getClass().getName() + " is not supported");
}
initManifestPermission(activity);
for (String permission : mPermissions) {
if (!sManifestPermissionSet.contains(permission)) {
if (mResult != null) {
mResult.onDenied();
}
return;
}
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
if (mResult != null) {
mResult.onGranted();
}
return;
}
List<String> deniedPermissionList = getDeniedPermissions(activity, mPermissions);
if (deniedPermissionList.isEmpty()) {
if (mResult != null) {
mResult.onGranted();
}
return;
}
int requestCode = genRequestCode();
String[] deniedPermissions = deniedPermissionList.toArray(new String[deniedPermissionList.size()]);
requestPermissions(mObject, deniedPermissions, requestCode);
sResultArray.put(requestCode, mResult);
}
public static void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
Result result = sResultArray.get(requestCode);
if (result == null) {
return;
}
sResultArray.remove(requestCode);
for (int grantResult : grantResults) {
if (grantResult != PackageManager.PERMISSION_GRANTED) {
result.onDenied();
return;
}
}
result.onGranted();
}
@TargetApi(Build.VERSION_CODES.M)
private static void requestPermissions(Object object, String[] permissions, int requestCode) {
if (object instanceof Activity) {
((Activity) object).requestPermissions(permissions, requestCode);
} else if (object instanceof Fragment) {
((Fragment) object).requestPermissions(permissions, requestCode);
}
}
private static List<String> getDeniedPermissions(Context context, String[] permissions) {
List<String> deniedPermissionList = new ArrayList<>();
for (String permission : permissions) {
if (ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
deniedPermissionList.add(permission);
}
}
return deniedPermissionList;
}
private static synchronized void initManifestPermission(Context context) {
if (sManifestPermissionSet == null) {
sManifestPermissionSet = new HashSet<>();
try {
PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_PERMISSIONS);
String[] permissions = packageInfo.requestedPermissions;
Collections.addAll(sManifestPermissionSet, permissions);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
}
private static Activity getActivity(Object object) {
if (object != null) {
if (object instanceof Activity) {
return (Activity) object;
} else if (object instanceof Fragment) {
return ((Fragment) object).getActivity();
}
}
return null;
}
private static int genRequestCode() {
return sRequestCode.incrementAndGet();
}
}
总结
本文主要介绍了如何快速、简单的适配 Android 6.0 运行时权限,虽然写的比较晚了,但还是希望能帮到大家。
如果你在阅读本文时发现什么问题或者纰漏,或者你有不同的看法,欢迎指出!