判断是否授权CheckPermisionUtil工具类

因为AppOpsManager方法public int checkOp(int op, int uid, String packageName)是隐藏的,所以需要反射进行来调用该方法,然后传入相应权限参数,可以判断是否已经授权,比如源码中就是读写短信的2个例子

  /** @hide */
    public static final int OP_READ_SMS = 14;
    /** @hide */
    public static final int OP_WRITE_SMS = 15;


CheckPermisionUtil

package com.example.permission;

import java.lang.reflect.Method;

import android.annotation.SuppressLint;
import android.app.AppOpsManager;
import android.content.Context;
import android.content.Intent;
import android.os.Process;
import android.provider.Settings;
import android.util.Log;

public class CheckPermisionUtil {

	private static final String[] type = { "int", "int", "string" };

	/**
	 * 反射方法获取返回值结果 public int checkOp(int op, int uid, String packageName)
	 */
	@SuppressLint("InlinedApi")
	private static int getPermisionResult(Context context, int permValue,
			String methodName) {
		int check = 0;
		try {
			AppOpsManager aom = (AppOpsManager) context
					.getSystemService(Context.APP_OPS_SERVICE);
			Class<? extends AppOpsManager> c = aom.getClass();
			Class<?> paramsTypes[] = ReflectUtil.getMethodClass(type);
			Method method = c.getMethod(methodName, paramsTypes);
			String[] params = { "" + permValue, "" + Process.myUid(),
					context.getPackageName() };
			Object arglist[] = ReflectUtil.getMethodObject(type, params);
			check = (Integer) method.invoke(aom, arglist);
		} catch (Exception e) {
			e.printStackTrace();
			Log.i("TAG", "--->getPermisionResult exception");
		}
		return check;
	}

	/**
	 * 判断某个权限是否被授权
	 */
	@SuppressLint("InlinedApi")
	public static boolean isPermisionGranted(Context context, int permValue) {
		boolean result = true;
		switch (getPermisionResult(context, permValue, "checkOp")) {
		case AppOpsManager.MODE_IGNORED:// 被禁止掉
			result = false;
			break;
		default:
			result = true;// 允许和需要确认不需要做什么操作,故都返回true
			break;
		}
		return result;
	}

	/**
	 * 打开系统安全设置界面
	 */
	public static void openSecurityPager(Context context) {
		openSystemPager(context, Settings.ACTION_SECURITY_SETTINGS);
	}

	/**
	 * 打开设置界面
	 */
	public static void openSettingPager(Context context) {
		openSystemPager(context, Settings.ACTION_SETTINGS);
	}

	/**
	 * 打开系统界面
	 */
	public static void openSystemPager(Context context, String action) {
		Intent intent = new Intent();
		intent.setAction(action);
		context.startActivity(intent);
	}

	/**
	 * 检测短信编写权限是否被许可
	 */
	public static boolean isSmsWritePermisionGranted(Context context) {
		return isPermisionGranted(context, PermisionValue.OP_WRITE_SMS);
	}

	/**
	 * 检测短信接收权限是否被许可
	 */
	public static boolean isSmsReceivePermisionGranted(Context context) {
		return isPermisionGranted(context, PermisionValue.OP_RECEIVE_SMS);
	}

	/**
	 * 检测短信读取权限是否被许可
	 */
	public static boolean isSmsReadPermisionGranted(Context context) {
		return isPermisionGranted(context, PermisionValue.OP_READ_SMS);
	}

	/**
	 * 检测短信发送是否被许可
	 */
	public static boolean isSmsSendPermisionGranted(Context context) {
		return isPermisionGranted(context, PermisionValue.OP_SEND_SMS);
	}

	/**
	 * 检测打开相机权限是否被许可
	 */
	public static boolean isCameraPermisionGranted(Context context) {
		return isPermisionGranted(context, PermisionValue.OP_CAMERA);
	}

	/**
	 * 检测悬浮窗权限是否被许可
	 */
	public static boolean isFloatWindowPermisionGranted(Context context) {
		return isPermisionGranted(context,
				PermisionValue.OP_SYSTEM_ALERT_WINDOW);
	}

	/**
	 * 检测读取联系人权限是否被许可
	 */
	public static boolean isReadContactPermisionGranted(Context context) {
		return isPermisionGranted(context, PermisionValue.OP_READ_CONTACTS);
	}

	/**
	 * 检测添加联系人权限是否被许可
	 */
	public static boolean isWriteContactPermisionGranted(Context context) {
		return isPermisionGranted(context, PermisionValue.OP_WRITE_CONTACTS);
	}

	/**
	 * 检测直接拨号权限是否被许可(无界面拨号)
	 */
	public static boolean isCallPhonePermisionGranted(Context context) {
		return isPermisionGranted(context, PermisionValue.OP_CALL_PHONE);
	}

	/**
	 * 检测录制音频权限是否被许可
	 */
	public static boolean isRecordAudioPermisionGranted(Context context) {
		return isPermisionGranted(context, PermisionValue.OP_RECORD_AUDIO);
	}

	/**
	 * 检测读取通话记录权限是否被许可
	 */
	public static boolean isReadCallLogPermisionGranted(Context context) {
		return isPermisionGranted(context, PermisionValue.OP_READ_CALL_LOG);
	}

	/**
	 * 检测编写通话记录权限是否被许可
	 */
	public static boolean isWriteCallLogPermisionGranted(Context context) {
		return isPermisionGranted(context, PermisionValue.OP_WRITE_CALL_LOG);
	}

}

PermisionValue

package com.example.permission;

import java.util.HashMap;
import java.util.Map;

public class PermisionValue {
	
	public static final Map<Integer, String> permisionMap = new HashMap<Integer, String>();
	
	public static final int OP_GRANTED = 0X01;//许可
	public static final int OP_NO_GRANTED = 0X02;//禁止
	public static final int OP_UNSURE_GRANTED = 0X03;//需要确认
	
	public static final int OP_READ_CONTACTS = 4;
	public static final int OP_WRITE_CONTACTS = 5;
	public static final int OP_READ_CALL_LOG = 6;
	public static final int OP_WRITE_CALL_LOG = 7;
	public static final int OP_CALL_PHONE = 13;
	public static final int OP_READ_SMS = 14;
	public static final int OP_WRITE_SMS = 15;
	public static final int OP_RECEIVE_SMS = 16;
	public static final int OP_SEND_SMS = 20;
	public static final int OP_SYSTEM_ALERT_WINDOW = 24;
	public static final int OP_CAMERA = 26;
	public static final int OP_RECORD_AUDIO = 27;
	public static final int OP_PLAY_AUDIO = 28;
	
	
	static{
		permisionMap.put(OP_GRANTED, "");
		permisionMap.put(OP_WRITE_SMS, "编辑短信权限");
		permisionMap.put(OP_RECEIVE_SMS, "接收短信权限");
		permisionMap.put(OP_READ_SMS, "读取短信权限");
		permisionMap.put(OP_SEND_SMS, "发送短信权限");
		permisionMap.put(OP_CAMERA, "拍照权限");
		permisionMap.put(OP_SYSTEM_ALERT_WINDOW, "悬浮窗权限");
		permisionMap.put(OP_READ_CONTACTS, "读取联系人权限");
		permisionMap.put(OP_WRITE_CONTACTS, "添加联系人权限");
		permisionMap.put(OP_CALL_PHONE, "拨号权限");
		permisionMap.put(OP_RECORD_AUDIO, "录音权限");
		permisionMap.put(OP_READ_CALL_LOG, "读取通话记录权限");
		permisionMap.put(OP_WRITE_CALL_LOG, "修改通话记录权限");
	}
	
}

以上只是列出了一部分,AppOpsManager中可以去查看有哪些权限


ReflectUtil

package com.example.permission;

import android.annotation.SuppressLint;

public class ReflectUtil {

	// 获取参数类型Class[]的方法
	@SuppressWarnings("rawtypes")
	public static Class[] getMethodClass(String[] type) {
		Class[] cs = new Class[type.length];
		for (int i = 0; i < cs.length; i++) {
			if (!type[i].trim().equals("") || type[i] != null) {
				if (type[i].equals("int") || type[i].equals("Integer")) {
					cs[i] = Integer.TYPE;
				} else if (type[i].equals("float") || type[i].equals("Float")) {
					cs[i] = Float.TYPE;
				} else if (type[i].equals("double") || type[i].equals("Double")) {
					cs[i] = Double.TYPE;
				} else if (type[i].equals("boolean") || type[i].equals("Boolean")) {
					cs[i] = Boolean.TYPE;
				} else {
					cs[i] = String.class;
				}
			}
		}
		return cs;
	}

	// 获取参数Object[]的方法
	@SuppressLint("UseValueOf")
	public static Object[] getMethodObject(String[] type, String[] param) {
		Object[] obj = new Object[param.length];
		for (int i = 0; i < obj.length; i++) {
			if (!param[i].trim().equals("") || param[i] != null) {
				if (type[i].equals("int") || type[i].equals("Integer")) {
					obj[i] = new Integer(param[i]);
				} else if (type[i].equals("float") || type[i].equals("Float")) {
					obj[i] = new Float(param[i]);
				} else if (type[i].equals("double") || type[i].equals("Double")) {
					obj[i] = new Double(param[i]);
				} else if (type[i].equals("boolean") || type[i].equals("Boolean")) {
					obj[i] = new Boolean(param[i]);
				} else {
					obj[i] = param[i];
				}
			}
		}
		return obj;
	}
}

MainActivity

package com.example.permission;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		CheckPermisionUtil.openSecurityPager(this);
		CheckPermisionUtil.openSettingPager(this);
		
		boolean isSmsWritePermisionGranted = CheckPermisionUtil.isSmsWritePermisionGranted(this);
		boolean isSmsReceivePermisionGranted = CheckPermisionUtil.isSmsReceivePermisionGranted(this);
		boolean isSmsReadPermisionGranted = CheckPermisionUtil.isSmsReadPermisionGranted(this);
		boolean isSmsSendPermisionGranted = CheckPermisionUtil.isSmsSendPermisionGranted(this);
		boolean isCameraPermisionGranted = CheckPermisionUtil.isCameraPermisionGranted(this);
		boolean isFloatWindowPermisionGranted = CheckPermisionUtil.isFloatWindowPermisionGranted(this);
		boolean isReadContactPermisionGranted = CheckPermisionUtil.isReadContactPermisionGranted(this);
		boolean isWriteContactPermisionGranted = CheckPermisionUtil.isWriteContactPermisionGranted(this);
		boolean isCallPhonePermisionGranted = CheckPermisionUtil.isCallPhonePermisionGranted(this);
		boolean isRecordAudioPermisionGranted = CheckPermisionUtil.isRecordAudioPermisionGranted(this);
		boolean isReadCallLogPermisionGranted = CheckPermisionUtil.isReadCallLogPermisionGranted(this);
		boolean isWriteCallLogPermisionGranted = CheckPermisionUtil.isWriteCallLogPermisionGranted(this);
		
		Log.i("TAG", "isSmsWritePermisionGranted:"+isSmsWritePermisionGranted);
		Log.i("TAG", "isSmsReceivePermisionGranted:"+isSmsReceivePermisionGranted);
		Log.i("TAG", "isSmsReadPermisionGranted:"+isSmsReadPermisionGranted);
		Log.i("TAG", "isSmsSendPermisionGranted:"+isSmsSendPermisionGranted);
		Log.i("TAG", "isCameraPermisionGranted:"+isCameraPermisionGranted);
		Log.i("TAG", "isFloatWindowPermisionGranted:"+isFloatWindowPermisionGranted);
		Log.i("TAG", "isReadContactPermisionGranted:"+isReadContactPermisionGranted);
		Log.i("TAG", "isWriteContactPermisionGranted:"+isWriteContactPermisionGranted);
		Log.i("TAG", "isCallPhonePermisionGranted:"+isCallPhonePermisionGranted);
		Log.i("TAG", "isRecordAudioPermisionGranted:"+isRecordAudioPermisionGranted);
		Log.i("TAG", "isReadCallLogPermisionGranted:"+isReadCallLogPermisionGranted);
		Log.i("TAG", "isWriteCallLogPermisionGranted:"+isWriteCallLogPermisionGranted);
	}

}


你可能感兴趣的:(判断是否授权CheckPermisionUtil工具类)