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 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;
}
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);
}
} else {
if(permissionCallback!=null){
permissionCallback.permissionSuccess(requestCode);
}else {
doExecuteSuccess(obj, requestCode);
}
}
}
public interface PermissionCallback{
//请求权限成功
void permissionSuccess(int requsetCode);
//请求权限失败
void permissionFail(int requestCode);
}
}
四、本地调用权限框架
1、添加依赖,在主项目的build.gradle文件的dependencies{}配置里,添加如下语句:
dependencies {
//集成权限请求框架
implementation project(‘:library’)
}
2、简单示例用法:
public class MainActivity extends AppCompatActivity {
//联系人请求码
private final int REQUEST_CONTACT = 100;
//存储请求码
private final int REQUEST_STORAGE = 200;
//相机请求码
private final int REQUEST_CAMERA = 300;
最后
感觉现在好多人都在说什么安卓快凉了,工作越来越难找了。又是说什么程序员中年危机啥的,为啥我这年近30的老农根本没有这种感觉,反倒觉得那些贩卖焦虑的都是瞎j8扯谈。当然,职业危机意识确实是要有的,但根本没到那种草木皆兵的地步好吗?
Android凉了都是弱者的借口和说辞。虽然 Android 没有前几年火热了,已经过去了会四大组件就能找到高薪职位的时代了。这只能说明 Android 中级以下的岗位饱和了,现在高级工程师还是比较缺少的,很多高级职位给的薪资真的特别高(钱多也不一定能找到合适的),所以努力让自己成为高级工程师才是最重要的。
所以,最后这里放上我耗时两个月,将自己8年Android开发的知识笔记整理成的Android开发者必知必会系统学习资料笔记,上述知识点在笔记中都有详细的解读,里面还包含了腾讯、字节跳动、阿里、百度2019-2021面试真题解析,并且把每个技术点整理成了视频和PDF(知识脉络 + 诸多细节)。
以上全套学习笔记面试宝典,吃透一半保你可以吊打面试官,只有自己真正强大了,有核心竞争力,你才有拒绝offer的权力,所以,奋斗吧!骚年们!千里之行,始于足下。种下一颗树最好的时间是十年前,其次,就是现在。
最后,赠与大家一句诗,共勉!
不驰于空想,不骛于虚声。不忘初心,方得始终。
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
ass MainActivity extends AppCompatActivity {
//联系人请求码
private final int REQUEST_CONTACT = 100;
//存储请求码
private final int REQUEST_STORAGE = 200;
//相机请求码
private final int REQUEST_CAMERA = 300;
最后
感觉现在好多人都在说什么安卓快凉了,工作越来越难找了。又是说什么程序员中年危机啥的,为啥我这年近30的老农根本没有这种感觉,反倒觉得那些贩卖焦虑的都是瞎j8扯谈。当然,职业危机意识确实是要有的,但根本没到那种草木皆兵的地步好吗?
Android凉了都是弱者的借口和说辞。虽然 Android 没有前几年火热了,已经过去了会四大组件就能找到高薪职位的时代了。这只能说明 Android 中级以下的岗位饱和了,现在高级工程师还是比较缺少的,很多高级职位给的薪资真的特别高(钱多也不一定能找到合适的),所以努力让自己成为高级工程师才是最重要的。
所以,最后这里放上我耗时两个月,将自己8年Android开发的知识笔记整理成的Android开发者必知必会系统学习资料笔记,上述知识点在笔记中都有详细的解读,里面还包含了腾讯、字节跳动、阿里、百度2019-2021面试真题解析,并且把每个技术点整理成了视频和PDF(知识脉络 + 诸多细节)。
[外链图片转存中…(img-41oTfQal-1714448873469)]
以上全套学习笔记面试宝典,吃透一半保你可以吊打面试官,只有自己真正强大了,有核心竞争力,你才有拒绝offer的权力,所以,奋斗吧!骚年们!千里之行,始于足下。种下一颗树最好的时间是十年前,其次,就是现在。
最后,赠与大家一句诗,共勉!
不驰于空想,不骛于虚声。不忘初心,方得始终。
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!