android通过代码获取华为手机的EMUI系统版本号

因为app中用到华为推送,但是华为推送在不同版本上是存在不同问题的,需要单独来处理。
那么最基本的问题是要获取EMUI系统的版本号。

上网翻了很多博客帖子,基本上是在获取root权限下去读取/system/build.prop文件的内容,获取Emui版本号。但是,不是所有用户的手机都root了。

public static double getEMUI2() {
    Properties properties = new Properties();
    try {
        properties.load(new FileInputStream(new File(Environment.getRootDirectory(), "build.prop")));
    } catch (IOException e) {
        e.printStackTrace();
    }
    String property = properties.getProperty("ro.build.version.emui", "");
    if (!TextUtils.isEmpty(property)) {
        String substring = property.substring(property.length() - 3, property.length());
        Double num = Double.valueOf(substring);
        return num;
    }
    return 0;
}

此方法是直接去读取系统文件获取版本号。但是是在手机root的情况下才能获取到。
用命令行打开此系统文件的方式是:

结果就可以看到该设备的所有信息

正片:
命令行获取 adb shell getprop ,经本人用公司现有的测试手机测试,此命令行不管设备有没有root,都可以获取到设备信息
结果

通过代码可得到

可以通过反射的方式获取

public static String getEMUI() {
    Class<?> classType = null;
    String buildVersion = null;
    try {
        classType = Class.forName("android.os.SystemProperties");
        Method getMethod = classType.getDeclaredMethod("get", new Class<?>[]{String.class});
        buildVersion = (String) getMethod.invoke(classType, new Object[]{"ro.build.version.emui"});
    } catch (Exception e) {
        e.printStackTrace();
    }
    return buildVersion;
}
posted @ 2017-03-31 23:10  吹泡泡`OOO  阅读(6818)  评论(0编辑  收藏  举报