此方法可以判断MIUI系统是否打开悬浮窗权限
1、判断是否是MIUI系统
private boolean isMIUI() {
String brand = Build.BRAND.toString();
if ("Xiaomi".equalsIgnoreCase(brand)) {
return true;
}
return false;
}
2、判断是否打开权限
private static ApplicationInfo localApplicationInfo1 = null;
private int op(Context context) {
if (Build.VERSION.SDK_INT >= 19){
try {
Object localObject = context
.getSystemService("appops");
Class localClass = localObject.getClass();
Class[] arrayOfClass = new Class[3];
arrayOfClass[0] = Integer.TYPE;
arrayOfClass[1] = Integer.TYPE;
arrayOfClass[2] = String.class;
Method localMethod = localClass.getMethod("checkOp",
arrayOfClass);
Object[] arrayOfObject = new Object[3];
arrayOfObject[0] = Integer.valueOf(24);
arrayOfObject[1] = Integer.valueOf(Binder.getCallingUid());
arrayOfObject[2] = context.getPackageName();
int j = ((Integer) localMethod.invoke(localObject,
arrayOfObject)).intValue();
if (j == 0)
return 1;
return 0;
} catch (Throwable localThrowable2) {
return -1;
}
}else{//19以下的版本的特殊处理
ActivityManager am = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);
ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
if(!"你的app的包名".equalsIgnoreCase(cn.getPackageName())){
try {
ApplicationInfo localApplicationInfo2 = FS
.getInstance()
.getPackageManager()
.getApplicationInfo(context.getPackageName(),
0);
localApplicationInfo1 = localApplicationInfo2;
if (localApplicationInfo1 != null) {
if ((0x8000000 & localApplicationInfo1.flags) == 0)
return 0;
}
} catch (Throwable localThrowable1) {
while (true)
localApplicationInfo1 = null;
}
}
return -1;
}
}
private boolean checkOp(Context context) {
try {
if (AppOpsManager.MODE_ALLOWED == op(context)) {
return true;
} else {
return false;
}
} catch (Exception e) {
return false;
}
}
public boolean isMiuiFloaAllowed(Context context) {
if (isMIUI()) {
return checkOp(context);
} else {
return false;
}
}