JAVA代码:

 public boolean isHarmony(Context context) {
        try {
            int id = Resources.getSystem().getIdentifier("config_os_brand", "string", "android");
            return context.getString(id).equals("harmony");
        } catch (Exception e) {
            return false;
        }
    }

Kotlin代码:

fun isHarmony(context: Context): Boolean {
    return runCatching {
        val id = context.resources.getIdentifier("config_os_brand", "string", "android")
        context.getString(id) == "harmony"
    }.getOrDefault(false)
}

 通过以上代码逻辑,可以准确的判断出当前华为设备是鸿蒙系统还是EMUI版本的Android系统

Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐