其他分享
首页 > 其他分享> > Android组件化开发实战:封装权限管理请求框架,移动应用与开发就业方向

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};

/**

*/

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;

/**

*/

public class PermissionUtils {

/**

*/

public static boolean judgeVersion() {

return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M;

}

/**

*/

@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;

}

/**

*/

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;

}

/**

*/

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

/**

*/

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;

/**

*/

@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;

/**

*/

public class OmgPermission {

//权限集合

private String[] mPermissions;

//请求码

private int mRequestCode;

//对象

private Object object;

//权限回调方法

private static PermissionCallback permissionCallback;

/**

*/

private OmgPermission(Object object) {

this.object = object;

}

/**

*/

public static OmgPermission with(Activity activity){

return new OmgPermission(activity);

}

public static OmgPermission with(Fragment fragment){

return new OmgPermission(fragment);

}

/**

*/

public OmgPermission permissions(String… permissions){

this.mPermissions = permissions;

return this;

}

/**

*/

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);

}

/**

*/

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 });

}

/**

*/

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 });

}

/**

*/

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 });

}

/**

*/

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 });

}

/**

*/

@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);

}

/**

*/

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