Android组件化开发实战:封装权限管理请求框架

本文介绍了Android组件化开发中如何封装一个权限管理请求框架。通过创建PermissionUtils工具类,实现权限判断、寻找未授权权限、回调接口等功能。还提供了OmgPermission对象类,用于处理权限请求并支持成功和失败的回调接口。通过示例展示了如何在MainActivity中使用此框架进行权限申请和回调处理。
摘要由CSDN通过智能技术生成

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 requestCode == 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;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值