米UI弹窗的问题

因为小米对系统的深度定制,所以米UI有很多的问题和Android开发是不一样的,这里纪录一下我之前发现的问题。当升级到miui7的时候发现想要弹出系统级的弹窗(就是那种弹出之后怎么也消失不了的,除非关机,一般用于强制更新)已经不可能了,后来发现原因在于MIUI把这一项作为了系统权限。后来为了实现弹出这个弹窗只能引导用户来开启这个权限(如果我是用户一定想哭)。
步骤分为四步走 判断是不是小米 –> 判断MIUI版本 –> 判断有没有开启弹窗权限 –> 跳转到应用设置页

第一步 判断是不是小米

private boolean isMIUI() {
String brand = Build.BRAND.toString();
if ("Xiaomi".equalsIgnoreCase(brand)) {
    return true;
}
return false;
}

第二步 判断MIUI版本

  String miUiVersion = getSystemProperty("ro.miui.ui.version.name");

public static String getSystemProperty(String propName) {
String line;
BufferedReader input = null;
try {
    Process p = Runtime.getRuntime().exec("getprop " + propName);
    input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024);
    line = input.readLine();
    input.close();
} catch (IOException ex) {
    Logger.e("", "Unable to read sysprop " + propName, ex);
    return null;
} finally {
    if (input != null) {
        try {
            input.close();
        } catch (IOException e) {
            Logger.e("", "Exception while closing InputStream", e);
        }
    }
}
return line;
}

第三步 判断有没有开启弹窗权限

if(isMiuiFloaAllowed(context)){
    getAppDetailSettingIntent(context);
}
public boolean isMiuiFloaAllowed(Context context) {
if (isMIUI()) {
    return checkOp(context);
} else {
    return false;
    }
}
private boolean checkOp(Context context) {
try {
    if (AppOpsManager.MODE_ALLOWED == op(context)) {
        return true;
    } else {
        return false;
    }
} catch (Exception e) {
    e.printStackTrace();
    return false;
    }
}
private static ApplicationInfo localApplicationInfo1 = null;
private int op(Context context) {
if (Build.VERSION.SDK_INT >= 19){
    try {
        Object localObject = context
                .getSystemService(APP_OPS_SERVICE);
        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(!"com.cdel.internal.mobile".equalsIgnoreCase(cn.getPackageName())){
        try {
            ApplicationInfo localApplicationInfo2 = BaseApplication
                    .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 void getAppDetailSettingIntent(Context context) {
Intent localIntent = new Intent();
localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= 9) {
    localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
    localIntent.setData(Uri.fromParts("package", BaseApplication.getInstance().getPackageName(), null));
} else if (Build.VERSION.SDK_INT <= 8) {
    localIntent.setAction(Intent.ACTION_VIEW);
    localIntent.setClassName("com.android.settings","com.android.settings.InstalledAppDetails");
    localIntent.putExtra("com.android.settings.ApplicationPkgName", BaseApplication.getInstance().getPackageName());
}
context.startActivity(localIntent);
Toast.makeText(context,"为了展示消息请您在<权限管理>--><显示悬浮窗>设置悬浮窗权限",Toast.LENGTH_LONG).show();
}
坚持原创技术分享,您的支持将鼓励我继续创作!