Android利用Java反射获取用户手机的rom定制系统版本及版本号,EMUI,MIUI,ColorOS,FunthouchOS等
前言
现在手机厂商都推出了自己的基于Android的UI系统,比如小米手机的MIUI,华为/荣耀的EMUI,OPPO的Color OS等等…
如何在代码中获得这些信息还是比较麻烦的。本人使用java反射拿到的该信息。
本人花费了大量的时间在网络上搜集到如何获取这些信息,在这里分享给需要的人,少走弯路,也欢迎大家在评论区补充出来。
提示:以下是本篇文章正文内容,下面案例可供参考
正文
代码中用到的字符串判空工具类就不再展示了。重要的信息是获取rom系统类型的KEY。
代码如下:
CustomOSUtils.getCustomOS(Build.BRAND);
CustomOSUtils.getCustomOSVersion(Build.BRAND);
import android.os.Build;
import java.lang.reflect.Method;
public class CustomOSUtils {
private static String customOS = "";
private static String customOSVersion = "";
private static final String KEY_HARMONYOS_VERSION_NAME = "hw_sc.build.platform.version";
private static final String KEY_EMUI_VERSION_NAME = "ro.build.version.emui";
private static final String KEY_MAGICUI_VERSION = "ro.build.version.magic";
private static final String KEY_MIUI_VERSION_NAME = "ro.miui.ui.version.name";
private static final String KEY_COLOROS_VERSION_NAME = "ro.build.version.opporom";
private static final String KEY_VIVO_VERSION_NAME = "ro.vivo.os.name";
private static final String KEY_VIVO_VERSION = "ro.vivo.os.version";
private static final String KEY_ONEPLUS_VERSION_NAME = "ro.rom.version";
private static final String KEY_FLYME_VERSION_NAME = "ro.build.display.id";
private static final String KEY_NUBIA_VERSION_NAME = "ro.build.nubia.rom.name";
private static final String KEY_NUBIA_VERSION_CODE = "ro.build.nubia.rom.code";
private static String getSystemPropertyValue(String key) {
try {
Class<?> classType = Class.forName("android.os.SystemProperties");
Method getMethod = classType.getDeclaredMethod("get", String.class);
String value = (String) getMethod.invoke(classType, new Object[]{key});
return value;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private static boolean isHarmonyOS() {
try {
Class<?> classType = Class.forName("com.huawei.system.BuildEx");
Method getMethod = classType.getMethod("getOsBrand");
String value = (String) getMethod.invoke(classType);
return !StrUtils.isEmpty(value);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
private static String getHarmonySystemPropertyValue() {
try {
Class<?> classType = Class.forName("com.huawei.system.BuildEx");
Method getMethod = classType.getMethod("getOsBrand");
String value = (String) getMethod.invoke(classType);
return value;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static String getPhoneSystem(String phoneBrand) {
if (StrUtils.isEmpty(customOS)) {
setCustomOSInfo(phoneBrand);
}
return customOS + customOSVersion;
}
private static boolean isMagicUI() {
return false;
}
public static String getCustomOS(String phoneBrand) {
if (StrUtils.isEmpty(customOS)) {
setCustomOSInfo(phoneBrand);
}
return customOS;
}
public static String getCustomOSVersion(String phoneBrand) {
if (StrUtils.isEmpty(customOS)) {
setCustomOSInfo(phoneBrand);
}
return customOSVersion;
}
public static String getCustomOSVersionSimple(String phoneBrand) {
String customOSVersionSimple = customOSVersion;
if (StrUtils.isEmpty(customOS)) {
getCustomOSVersion(phoneBrand);
}
if (customOSVersion.contains(".")){
int index = customOSVersion.indexOf(".");
customOSVersionSimple = customOSVersion.substring(0,index);
}
return customOSVersionSimple;
}
public static String deleteSpaceAndToUpperCase(String str) {
if (StrUtils.isEmpty(str)) {
return "";
}
return str.replaceAll(" ", "").toUpperCase();
}
private static void setCustomOSInfo(String phoneBrand) {
try {
switch (deleteSpaceAndToUpperCase(phoneBrand)) {
case "HUAWEI":
if (isHarmonyOS()) {
customOSVersion = getSystemPropertyValue(KEY_HARMONYOS_VERSION_NAME);
customOS = "HarmonyOS";
} else {
customOS = "EMUI";
customOSVersion = getSystemPropertyValue(KEY_EMUI_VERSION_NAME);;
}
break;
case "HONOR":
if (isHarmonyOS()) {
customOS = "HarmonyOS";
if (!StrUtils.isEmpty(getSystemPropertyValue(KEY_HARMONYOS_VERSION_NAME))){
customOSVersion = getSystemPropertyValue(KEY_HARMONYOS_VERSION_NAME);;
} else {
customOSVersion = "";
}
} else if (!StrUtils.isEmpty(getSystemPropertyValue(KEY_MAGICUI_VERSION))) {
customOS = "MagicUI";
customOSVersion = getSystemPropertyValue(KEY_MAGICUI_VERSION);
} else {
customOS = "EMUI";
customOSVersion = getSystemPropertyValue(KEY_EMUI_VERSION_NAME);
}
break;
case "XIAOMI":
case "REDMI":
customOS = "MIUI";
customOSVersion = getSystemPropertyValue(KEY_MIUI_VERSION_NAME);
break;
case "REALME":
case "OPPO":
customOS = "ColorOS";
customOSVersion = getSystemPropertyValue(KEY_COLOROS_VERSION_NAME);
break;
case "VIVO":
customOS = "Funtouch";
customOSVersion = getSystemPropertyValue(KEY_VIVO_VERSION);
break;
case "ONEPLUS":
customOS = "HydrogenOS";
customOSVersion = getSystemPropertyValue(KEY_ONEPLUS_VERSION_NAME);
break;
case "MEIZU":
customOS = "Flyme";
customOSVersion = getSystemPropertyValue(KEY_FLYME_VERSION_NAME);
break;
case "NUBIA":
customOS = getSystemPropertyValue(KEY_NUBIA_VERSION_NAME);
customOSVersion = getSystemPropertyValue(KEY_NUBIA_VERSION_CODE);
break;
default:
customOS = "Android";
customOSVersion = Build.VERSION.RELEASE;
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
结语
目前作者只收集到了如上几种机型的rom定制系统的类型和版本号的识别方法。可以看到有些机型和系统是没有的,存在的问题有:
- VIVO手机会全部识别成Funtouch,识别不到OriginOS
- realme手机会全部识别成ColorOS, 识别不到realme ui
- 一加手机会全部识别成HydrogeOS, 没有遇到过OxygenOS
- 三星手机未作识别,会返回原生安卓系统
如果有读者可以识别出以上系统,可以留言在评论区。
|