检查是否有权限
6.0及以上检查方法
PackageManager.checkPermission(String permName, String pkgName)
或
ContextCompat.checkSelfPermission(Context context, String permission)
6.0以下检查方法
AppOpsManager.checkOp(String op, int uid, String packageName)
public static boolean checkCallPhonePermission(Context context){
try {
AppOpsManager manager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
Class<?> clazz = AppOpsManager.class;
Field sysAlert = clazz.getField("OP_CALL_PHONE");
int op = sysAlert.getInt(manager);
Method method = clazz.getDeclaredMethod("checkOp", int.class, int.class, String.class);
int mode = (int) method.invoke(manager, op, Binder.getCallingUid(), context.getPackageName());
return AppOpsManager.MODE_ALLOWED == mode;
} catch (Exception ex) {
ex.printStackTrace();
}
return true;
}
网友评论