Android代码中判断是否为鸿蒙系统、获取鸿蒙系统版本号

/**
 * 是否为鸿蒙系统
 *
 * @return true为鸿蒙系统
 */
public static boolean isHarmonyOs() {
    try {
        Class<?> buildExClass = Class.forName("com.huawei.system.BuildEx");
        Object osBrand = buildExClass.getMethod("getOsBrand").invoke(buildExClass);
        return OS_HARMONY.equalsIgnoreCase(osBrand.toString());
    } catch (Throwable x) {
        return false;
    }
}

/**
 * 获取鸿蒙系统版本号
 *
 * @return 版本号
 */
public static String getHarmonyVersion() {
    return getProp("hw_sc.build.platform.version", "");
}

private static String getProp(String property, String defaultValue) {
    try {
        Class spClz = Class.forName("android.os.SystemProperties");
        Method method = spClz.getDeclaredMethod("get", String.class);
        String value = (String) method.invoke(spClz, property);
        if (TextUtils.isEmpty(value)) {
            return defaultValue;
        }
        return value;
    } catch (Throwable e) {
        e.printStackTrace();
    }
    return defaultValue;
}

猜你喜欢

转载自blog.csdn.net/chenzhengfeng/article/details/119868210
今日推荐