2
3

More than 3 years have passed since last update.

プログラムでOSはHarmony(HarmonyOS)かAndroidか判断する方法

Posted at

ファーウェイは2021年7月23日にHUAWEI MatePad 11をリリースしました。このタブレットはHarmonyOSを搭載しています。AndroidのアプリもHarmonyOSのアプリも正常動作します。

アプリの中でOSがHarmonyOSかAndroidか判断する場面があるかもしれないので、その方法を公開します。

public boolean isHarmonyOS() {
    try {
        Class classType = Class.forName("com.huawei.system.BuildEx");
        Method method = classType.getMethod("getOsBrand");
        ClassLoader classLoader = classType.getClassLoader();
        if (classLoader != null && classLoader.getParent() == null) {
            return "harmony".equals(method.invoke(classType));
        }
    } catch (ClassNotFoundException e) {
    } catch (NoSuchMethodException e) {
    } catch (Exception e) {
    }
    return false;
}
2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3