美文网首页
判断是否为鸿蒙系统方法

判断是否为鸿蒙系统方法

作者: 强大帅 | 来源:发表于2021-12-07 17:01 被阅读0次

可以通过这种方式进行判断

    /**
     * 判断当前是否为鸿蒙系统
     *
     * @return 是否是鸿蒙系统,是:true,不是:false
     */
    private static boolean isHarmonyOs() {
        try {
            Class<?> buildExClass = Class.forName("com.huawei.system.BuildEx");
            Object osBrand = buildExClass.getMethod("getOsBrand").invoke(buildExClass);
            if (osBrand == null) {
                return false;
            }
            return "harmony".equalsIgnoreCase(osBrand.toString());
        } catch (Throwable e) {
            return false;
        }
    }

也可以通过鸿蒙系统 version 是否为 null,反向判断是否为鸿蒙系统,顺便获取到鸿蒙版本号

    /**
     * 获取鸿蒙系统 Version
     *
     * @return HarmonyOS Version
     */
    public static String getHarmonyOSVersion() {
        String version = null;
        if (isHarmonyOs()) {
            version = getProp("hw_sc.build.platform.version", "");
        }
        return version;
    }

相关文章

网友评论

      本文标题:判断是否为鸿蒙系统方法

      本文链接:https://www.haomeiwen.com/subject/xkraxrtx.html