栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Android利用Java反射获取用户手机的rom定制系统及版本,EMUI,MIUI,ColorOS,FunthouchOS等

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android利用Java反射获取用户手机的rom定制系统及版本,EMUI,MIUI,ColorOS,FunthouchOS等

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 {
    
    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 {
                        //格式: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定制系统的类型和版本号的识别方法。可以看到有些机型和系统是没有的,存在的问题有:

  1. VIVO手机会全部识别成Funtouch,识别不到OriginOS
  2. realme手机会全部识别成ColorOS, 识别不到realme ui
  3. 一加手机会全部识别成HydrogeOS, 没有遇到过OxygenOS
  4. 三星手机未作识别,会返回原生安卓系统

如果有读者可以识别出以上系统,可以留言在评论区。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/324520.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号