动态权限工具类

一:PermissionUtils

package com.msd.standard.JT.util;

import android.app.Activity;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class PermissionUtils {
@Target(ElementType.METHOD)//注解的作用域
@Retention(RetentionPolicy.RUNTIME)//注解的有效时
public @interface PermissionHelper {
boolean permissionResult();
int requestCode();
}

public static void injectActivity(Activity activity, boolean permissionResult, int requestCode) {

    Class clazz = activity.getClass();

// clazz.getMethods();
Method[] methods = clazz.getDeclaredMethods();

    for (Method method : methods) {
        if (method.isAnnotationPresent(PermissionHelper.class)) {
            PermissionHelper annotation = method.getAnnotation(PermissionHelper.class);
            if (permissionResult == annotation.permissionResult() && annotation.requestCode() == requestCode) {
                try {
                    method.setAccessible(true);
                    method.invoke(activity);
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                break;
            }
        }
    }
}

}

二:baseActivity中
public class BaseActivity extends Activity {

private final String TAG = BaseActivity.class.getSimpleName();
private int permissionRequestCode = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}
public boolean checkPermission(String permission, int requestCode) {

    if (PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(this, permission)) {
        return true;
    } else {
        permissionRequestCode = requestCode;
        ActivityCompat.requestPermissions(this, new String[]{permission}, requestCode);
        return false;
    }

}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    Log.i(TAG, " == onRequestPermissionsResult == ");
    if (grantResults.length > 0 ) {
        PermissionUtils.injectActivity(this, grantResults[0] == PackageManager.PERMISSION_GRANTED, permissionRequestCode);
    }
}

}
三具体调用
public class TestActivity extends BaseActivity {

private final int PERMISSION_CAMERA = 100;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

相机权限
if(checkPermission(Manifest.permission.CAMERA, PERMISSION_CAMERA)){
toDoThing();
}
//手机唯一键获取,读取电话权限
if(checkPermission(Manifest.permission.READ_PHONE_STATE, PERMISSION_CAMERA)){
toDoThing();
}
}

@PermissionUtils.PermissionHelper(permissionResult = true, requestCode = PERMISSION_CAMERA)
private void toDoThing(){

}

@SuppressLint("MissingPermission")
@PermissionUtils.PermissionHelper(permissionResult = true, requestCode = PERMISSION_CAMERA)
private void toDoThing(){
TelephonyManager TelephonyMgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
szImei = TelephonyMgr.getDeviceId();
}
}

你可能感兴趣的:(动态权限工具类)