Android组件化开发实战:封装权限管理请求框架,移动应用与开发就业方向
作者:互联网
STORAGE = new String[]{};
PACKAGES=new String[]{};
NOTIFICATION=new String[]{};
ALERTWINDOW=new String[]{};
SETTINGS=new String[]{};
} else {
CALENDAR = new String[]{
Manifest.permission.READ_CALENDAR,
Manifest.permission.WRITE_CALENDAR};
CAMERA = new String[]{
Manifest.permission.CAMERA};
CONTACTS = new String[]{
Manifest.permission.READ_CONTACTS,
Manifest.permission.WRITE_CONTACTS,
Manifest.permission.GET_ACCOUNTS};
//Android10及以上版本,新增2种权限
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
LOCATION = new String[]{
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_BACKGROUND_LOCATION,//在后台获取位置(Android 10.0及以上)
Manifest.permission.ACCESS_MEDIA_LOCATION//读取照片中的地理位置(Android 10.0及以上)
};
}else{
LOCATION = new String[]{
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION};
}
MICROPHONE = new String[]{
Manifest.permission.RECORD_AUDIO};
//Android8以上版本PROCESS_OUTGOING_CALLS换成了ANSWER_PHONE_CALLS。
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
PHONE = new String[]{
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.CALL_PHONE,
Manifest.permission.READ_CALL_LOG,
Manifest.permission.WRITE_CALL_LOG,
Manifest.permission.USE_SIP,
Manifest.permission.ADD_VOICEMAIL,
Manifest.permission.ANSWER_PHONE_CALLS,//接听电话(Android8.0及以上)
Manifest.permission.READ_PHONE_NUMBERS//读取手机号码(Android8.0及以上)
};
}else {
PHONE = new String[]{
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.CALL_PHONE,
Manifest.permission.READ_CALL_LOG,
Manifest.permission.WRITE_CALL_LOG,
Manifest.permission.USE_SIP,
Manifest.permission.ADD_VOICEMAIL,
Manifest.permission.PROCESS_OUTGOING_CALLS};
}
SENSORS = new String[]{
Manifest.permission.BODY_SENSORS,
Manifest.permission.ACTIVITY_RECOGNITION};
SMS = new String[]{
Manifest.permission.SEND_SMS,
Manifest.permission.RECEIVE_SMS,
Manifest.permission.READ_SMS,
Manifest.permission.RECEIVE_WAP_PUSH,
Manifest.permission.RECEIVE_MMS};
/**
-
外部存储权限
-
Android11以上版本,存储权限统一用MANAGE_EXTERNAL_STORAGE
*/
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.R){
STORAGE = new String[]{
Manifest.permission.MANAGE_EXTERNAL_STORAGE};
}else {
STORAGE = new String[]{
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE};
}
//安装应用权限(Android8.0及以上)
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
PACKAGES=new String[]{Manifest.permission.REQUEST_INSTALL_PACKAGES};
}else{
PACKAGES = new String[]{};
}
//通知栏权限
NOTIFICATION=new String[]{Manifest.permission.ACCESS_NOTIFICATION_POLICY};
//悬浮窗权限
ALERTWINDOW=new String[]{Manifest.permission.SYSTEM_ALERT_WINDOW};
//系统设置权限
SETTINGS=new String[]{Manifest.permission.WRITE_SETTINGS};
}
}
}
2、创建PermissionUtils.java工具类,这个类主要是封装一些权限的通用方法,代码如下:
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import androidx.fragment.app.Fragment;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
/**
-
权限的工具类
-
@author 安阳 QQ:15577969
-
@version 1.0
-
@team 美奇软件开发工作室
-
@date 2020/11/23 13:09
*/
public class PermissionUtils {
/**
-
判断Android系统版本是否大于6.0
-
@return
*/
public static boolean judgeVersion() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M;
}
/**
-
从申请的权限中找出未授予的权限
-
@param activity
-
@param permission
-
@return
*/
@TargetApi(value = Build.VERSION_CODES.M)
public static List findDeniedPermissions(Activity activity, String… permission) {
List denyPermissions = new ArrayList<>();
for (String value : permission) {
if (activity.checkSelfPermission(value) != PackageManager.PERMISSION_GRANTED) {
denyPermissions.add(value);
}
}
return denyPermissions;
}
/**
-
寻找相应的注解方法
-
@param c1 要寻找的那个类
-
@param c2 响应的注解标记
-
@return
*/
public static List findAnnotationMethods(Class c1, Class<? extends Annotation> c2) {
List methods = new ArrayList<>();
for (Method method : c1.getDeclaredMethods()) {
if (method.isAnnotationPresent(c2)) {
methods.add(method);
}
}
return methods;
}
public static Method findMethodPermissionFailWithRequestCode(Class clazz, Class permissionFailClass, int requestCode) {
for (Method method : clazz.getDeclaredMethods()) {
if (method.isAnnotationPresent(permissionFailClass)) {
if (requestCode == method.getAnnotation(PermissionFail.class).requestCode()) {
return method;
}
}
}
return null;
}
/**
-
找到相应的注解方法(requestCode请求码与需要的一样)
-
@param m
-
@param c
-
@param requestCode
-
@return
*/
public static boolean isEqualRequestCodeFromAnntation(Method m, Class c, int requestCode) {
if (c.equals(PermissionFail.class)) {
return req
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
uestCode == m.getAnnotation(PermissionFail.class).requestCode();
} else if (c.equals(PermissionSuccess.class)) {
return requestCode == m.getAnnotation(PermissionSuccess.class).requestCode();
} else {
return false;
}
}
public static Method findMethodWithRequestCode(Class c, Class annotation, int requestCode) {
for (Method method : c.getDeclaredMethods()) {
if (method.isAnnotationPresent(annotation)) {
if (isEqualRequestCodeFromAnntation(method, annotation, requestCode)) {
return method;
}
}
}
return null;
}
public static Method findMethodPermissionSuccessWithRequestCode(Class c, Class permissionFailClass, int requestCode) {
for (Method method : c.getDeclaredMethods()) {
if (method.isAnnotationPresent(permissionFailClass)) {
if (requestCode == method.getAnnotation(PermissionSuccess.class).requestCode()) {
return method;
}
}
}
return null;
}
public static Activity getActivity(Object object) {
if (object instanceof Fragment) {
return ((Fragment) object).getActivity();
} else if (object instanceof Activity) {
return (Activity) object;
}
return null;
}
}
3、创建成功和失败的回调接口类,代码如下:
PermissionSuccess.java
/**
-
@author 安阳 QQ:15577969
-
@version 1.0
-
@team 美奇软件开发工作室
-
@date 2020/11/23 13:12
*/
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface PermissionSuccess {
int requestCode();
}
PermissionFail.java
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
-
@author 安阳 QQ:15577969
-
@version 1.0
-
@team 美奇软件开发工作室
-
@date 2020/11/23 13:12
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface PermissionFail {
int requestCode();
}
4、创建OmgPermission.java对象类,这个类就是我们封装的权限框架主体,代码如下:
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import androidx.fragment.app.Fragment;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
/**
-
动态权限的对象
-
@author 安阳 QQ:15577969
-
@version 1.0
-
@team 美奇软件开发工作室
-
@date 2020/11/23 12:56
*/
public class OmgPermission {
//权限集合
private String[] mPermissions;
//请求码
private int mRequestCode;
//对象
private Object object;
//权限回调方法
private static PermissionCallback permissionCallback;
/**
- 构造方法
*/
private OmgPermission(Object object) {
this.object = object;
}
/**
-
with函数是将某对象作为函数的参数,在函数块内可以通过 this 指代该对象。
-
返回值为函数块的最后一行或指定return表达式。
*/
public static OmgPermission with(Activity activity){
return new OmgPermission(activity);
}
public static OmgPermission with(Fragment fragment){
return new OmgPermission(fragment);
}
/**
-
获取权限组集合
-
@param permissions
-
@return
*/
public OmgPermission permissions(String… permissions){
this.mPermissions = permissions;
return this;
}
/**
-
添加请求码
-
@param requestCode
-
@return
*/
public OmgPermission addRequestCode(int requestCode){
this.mRequestCode = requestCode;
return this;
}
@TargetApi(value = Build.VERSION_CODES.M)
public void request(){
permissionCallback = null;
requestPermissions(object, mRequestCode, mPermissions);
}
@TargetApi(value = Build.VERSION_CODES.M)
public void request(PermissionCallback callback){
if(callback!=null) {
permissionCallback = callback;
}
requestPermissions(object, mRequestCode, mPermissions);
}
/**
-
活动请求权限
-
@param activity
-
@param requestCode
-
@param permissions
*/
public static void needPermission(Activity activity, int requestCode, String[] permissions){
permissionCallback = null;
requestPermissions(activity, requestCode, permissions);
}
public static void needPermission(Activity activity, int requestCode, String permission){
permissionCallback = null;
needPermission(activity, requestCode, new String[] { permission });
}
/**
-
活动请求权限,带回调方法
-
@param activity
-
@param requestCode
-
@param permissions
-
@param callback
*/
public static void needPermission(Activity activity, int requestCode, String[] permissions
,OmgPermission.PermissionCallback callback) {
if (callback != null) {
permissionCallback = callback;
}
requestPermissions(activity, requestCode, permissions);
}
public static void needPermission(Activity activity, int requestCode, String permission,PermissionCallback callback){
if (callback != null) {
permissionCallback = callback;
}
needPermission(activity, requestCode, new String[] { permission });
}
/**
-
碎片请求权限
-
@param fragment
-
@param requestCode
-
@param permissions
*/
public static void needPermission(Fragment fragment, int requestCode, String[] permissions){
permissionCallback = null;
requestPermissions(fragment, requestCode, permissions);
}
public static void needPermission(Fragment fragment, int requestCode, String permission){
permissionCallback = null;
needPermission(fragment, requestCode, new String[] { permission });
}
/**
-
碎片请求权限,带回调方法
-
@param fragment
-
@param requestCode
-
@param permissions
-
@param callback
*/
public static void needPermission(Fragment fragment, int requestCode, String[] permissions
,OmgPermission.PermissionCallback callback) {
if (callback != null) {
permissionCallback = callback;
}
requestPermissions(fragment, requestCode, permissions);
}
public static void needPermission(Fragment fragment, int requestCode, String permission,PermissionCallback callback){
if (callback != null) {
permissionCallback = callback;
}
needPermission(fragment, requestCode, new String[] { permission });
}
/**
-
请求权限
-
@param object
-
@param requestCode
-
@param permissions
*/
@TargetApi(value = Build.VERSION_CODES.M)
private static void requestPermissions(Object object, int requestCode, String[] permissions){
//判断系统版本是否大于6.0
if(!PermissionUtils.judgeVersion()) {
if (permissionCallback != null) {
permissionCallback.permissionSuccess(requestCode);
}else {
doExecuteSuccess(object, requestCode);
}
return;
}
List deniedPermissions = PermissionUtils.findDeniedPermissions(PermissionUtils.getActivity(object), permissions);
/**
- 先检查是否有没有授予的权限,有的话请求,没有的话就直接执行权限授予成功的接口/注解方法
*/
if(deniedPermissions.size() > 0){
if(object instanceof Activity){
((Activity)object).requestPermissions(deniedPermissions.toArray(new String[deniedPermissions.size()]), requestCode);
} else if(object instanceof Fragment){
((Fragment)object).requestPermissions(deniedPermissions.toArray(new String[deniedPermissions.size()]), requestCode);
} else {
throw new IllegalArgumentException(object.getClass().getName() + " is not supported");
}
} else {
if (permissionCallback != null) {
permissionCallback.permissionSuccess(requestCode);
}else {
doExecuteSuccess(object, requestCode);
}
}
}
private static void doExecuteSuccess(Object activity, int requestCode) {
Method executeMethod = PermissionUtils.findMethodWithRequestCode(activity.getClass(),
PermissionSuccess.class, requestCode);
executeMethod(activity, executeMethod);
}
private static void doExecuteFail(Object activity, int requestCode) {
Method executeMethod = PermissionUtils.findMethodWithRequestCode(activity.getClass(),
PermissionFail.class, requestCode);
executeMethod(activity, executeMethod);
}
private static void executeMethod(Object activity, Method executeMethod) {
if(executeMethod != null){
try {
if(!executeMethod.isAccessible()) executeMethod.setAccessible(true);
executeMethod.invoke(activity, new Object[]{});
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
public static void onRequestPermissionsResult(Activity activity, int requestCode, String[] permissions,
int[] grantResults) {
requestResult(activity, requestCode, permissions, grantResults);
}
public static void onRequestPermissionsResult(Fragment fragment, int requestCode, String[] permissions,
int[] grantResults) {
requestResult(fragment, requestCode, permissions, grantResults);
}
/**
-
回调接口不为空的话,先执行回调接口的方法,若为空,则寻找响应的注解方法。
-
@param obj
-
@param requestCode
-
@param permissions
-
@param grantResults
*/
private static void requestResult(Object obj, int requestCode, String[] permissions,
int[] grantResults){
List deniedPermissions = new ArrayList<>();
for(int i=0; i<grantResults.length; i++){
if(grantResults[i] != PackageManager.PERMISSION_GRANTED){
deniedPermissions.add(permissions[i]);
}
}
if(deniedPermissions.size() > 0){
if(permissionCallback!=null){
permissionCallback.permissionFail(requestCode);
}else {
doExecuteFail(obj, requestCode);
}
标签:封装,String,permission,param,Manifest,开发,requestCode,new,Android 来源: https://blog.csdn.net/flunsna/article/details/122155106