Android利用Java反射获取用户手机的rom定制系统版本及版本号,EMUI,MIUI,ColorOS,FunthouchOS等
- 前言
- 正文
- 结语
前言
现在手机厂商都推出了自己的基于Android的UI系统,比如小米手机的MIUI,华为/荣耀的EMUI,OPPO的Color OS等等…
如何在代码中获得这些信息还是比较麻烦的。本人使用java反射拿到的该信息。
本人花费了大量的时间在网络上搜集到如何获取这些信息,在这里分享给需要的人,少走弯路,也欢迎大家在评论区补充出来。
提示:以下是本篇文章正文内容,下面案例可供参考
正文
代码中用到的字符串判空工具类就不再展示了。重要的信息是获取rom系统类型的KEY。
代码如下:
//获取系统类型,Build.Brand是手机的品牌信息
CustomOSUtils.getCustomOS(Build.BRAND);
//获取系统版本号
CustomOSUtils.getCustomOSVersion(Build.BRAND);
import android.os.Build;
import java.lang.reflect.Method;
public class CustomOSUtils {
/**
* customOS默认值为"",如果识别出的手机厂商是预知的,会被重新赋值,如果未识别到该机型则返回原生安卓信息
*/
private static String customOS = "";
/**
* CustomOSVersion默认值为"",如果识别出的手机厂商是预知的,会被重新赋值成对应rom系统的版本号
* 如果未识别到该机型则返回原生安卓信息
*/
private static String customOSVersion = "";
/**
* HarmonyOS 系统输出的
* 格式:2.0.0
*/
private static final String KEY_HARMONYOS_VERSION_NAME = "hw_sc.build.platform.version";
/**
* EMUI系统输出的
* 格式:EmotionUI_8.0.0
*/
private static final String KEY_EMUI_VERSION_NAME = "ro.build.version.emui";
/**
* MagicUI系统输出的
* 格式:3.1.0
*/
private static final String KEY_MAGICUI_VERSION = "ro.build.version.magic";
/**
* MIUI系统输出的
* 格式:V12
*/
private static final String KEY_MIUI_VERSION_NAME = "ro.miui.ui.version.name";
/**
* OPPO手机ColorOS系统输出的
* 格式:9
*/
private static final String KEY_COLOROS_VERSION_NAME = "ro.build.version.opporom";
/**
* VIVO手机系统输出的
* name格式:funtouch
* version格式: 9
*/
private static final String KEY_VIVO_VERSION_NAME = "ro.vivo.os.name";
private static final String KEY_VIVO_VERSION = "ro.vivo.os.version";
/**
* OonPlus手机系统输出的
* 格式:Hydrogen OS 11.0.7.10.KB05
*/
private static final String KEY_ONEPLUS_VERSION_NAME = "ro.rom.version";
/**
* 魅族手机系统输出的
*/
private static final String KEY_FLYME_VERSION_NAME = "ro.build.display.id";
/**
* nubia手机系统输出的
*/
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";
/**
* 传入获取手机系统属性的key,可以得到rom系统版本信息
* @param key
* @return
*/
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;
}
}
/**
* 判断是否是华为鸿蒙系统,能否识别荣耀鸿蒙未知
*
* @return
*/
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;
}
}
/**
* 通过手机品牌信息获取手机rom系统+系统版本号
* @param phoneBrand
* @return
*/
public static String getPhoneSystem(String phoneBrand) {
if (StrUtils.isEmpty(customOS)) {
setCustomOSInfo(phoneBrand);
}
return customOS + customOSVersion;
}
private static boolean isMagicUI() {
return false;
}
/**
* 通过手机品牌信息获取手机rom系统
* @param phoneBrand
* @return
*/
public static String getCustomOS(String phoneBrand) {
if (StrUtils.isEmpty(customOS)) {
setCustomOSInfo(phoneBrand);
}
return customOS;
}
/**
* 通过手机品牌信息获取手机rom系统版本号
* @param phoneBrand
* @return
*/
public static String getCustomOSVersion(String phoneBrand) {
if (StrUtils.isEmpty(customOS)) {
setCustomOSInfo(phoneBrand);
}
return customOSVersion;
}
/**
* 通过手机品牌信息获取手机rom系统版本号,截取成大版本,比如2.0.0截成2
* @param phoneBrand
* @return
*/
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;
}
/**
* 删除字符串中的空格并全部转成大写
* @param str
* @return
*/
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 {
//格式:EmotionUI_8.0.0
customOS = "EMUI";
customOSVersion = getSystemPropertyValue(KEY_EMUI_VERSION_NAME);
}
break;
case "XIAOMI":
case "REDMI":
//格式:MIUIV12
customOS = "MIUI";
customOSVersion = getSystemPropertyValue(KEY_MIUI_VERSION_NAME);
break;
case "REALME":
case "OPPO":
//格式:ColorOSV2.1
customOS = "ColorOS";
customOSVersion = getSystemPropertyValue(KEY_COLOROS_VERSION_NAME);
break;
case "VIVO":
//格式:Funtouch9
customOS = "Funtouch";
customOSVersion = getSystemPropertyValue(KEY_VIVO_VERSION);
break;
case "ONEPLUS":
//格式:Hydrogen OS 11.0.7.10.KB05
customOS = "HydrogenOS";
customOSVersion = getSystemPropertyValue(KEY_ONEPLUS_VERSION_NAME);
break;
case "MEIZU":
//格式:Flyme 6.3.5.1G
customOS = "Flyme";
customOSVersion = getSystemPropertyValue(KEY_FLYME_VERSION_NAME);
break;
case "NUBIA":
//格式:nubiaUIV3.0
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
- 三星手机未作识别,会返回原生安卓系统
如果有读者可以识别出以上系统,可以留言在评论区。
补充:
用户@Abnew_L回复:
VIVO 可以通过获取 ro.vivo.os.build.display.id 返回 OriginOS 1.0 来识别是不是 OriginOs