标签:
支持原著:http://www.cnblogs.com/KennyXia/p/4332642.html
针对目前市面上存在大量的MIUI、EMUI的手机,作为开发人员,有时需要查看应用的悬浮窗是否开启,但是没有一个好的方法获取这个值,我和公司同事想到这个方法:
/** * 判断悬浮窗权限 * * @param context 上下文 */ public static boolean isFloatWindowOpAllowed(Context context) { final int version = Build.VERSION.SDK_INT; if (version >= 19) { return checkOp(context, 24);//24表示悬浮窗权限在AppOpsManager中 } else { //0x8000000表示1000000000000000000000000000如果&第28位所得值为1则该位置被置为1,悬浮窗打开 return (context.getApplicationInfo().flags & 0x8000000) == 1<<27; } } protected static boolean checkOp(Context context, int op) { final int version = Build.VERSION.SDK_INT; if (version >= 19) { AppOpsManager manager = (AppOpsManager) context.getSystemService("appops"); try { Object object = invokeMethod(manager, "checkOp", op, Binder.getCallingUid(), getPackageName(context)); return AppOpsManager.MODE_ALLOWED == (Integer) object; } catch (Exception e) { LogTool.e("CheckMIUI", e.toString()); } } else { LogTool.e("CheckMIUI", "Below API 19 cannot invoke!"); } return false; } public static String getPackageName(Context context) throws PackageManager.NameNotFoundException { PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0); return pInfo.packageName; } public static Object invokeMethod(AppOpsManager manager, String method, int op, int uid, String packageName){ Class c = manager.getClass(); try { Class[] classes = new Class[] {int.class, int.class, String.class}; Object[] x2 = {op, uid, packageName}; Method m = c.getDeclaredMethod(method, classes); return m.invoke(manager, x2); } catch (Exception e) { e.printStackTrace(); } return -1; }
标签:
原文地址:http://www.cnblogs.com/KennyXia/p/4332642.html