Android开发中-鸿蒙系统以及平行视界

测试设备:华为平板 HUAWEI MRX-W09 Android 10 API29

场景:在Android开发过程中一些用户反馈平板设备ui显示异常,通过沟通发现是鸿蒙系统平行世界导致。

判断是否是鸿蒙系统:
通过判断SystemCapability(api5开始)类是否存在来判断是否是鸿蒙

public static boolean isHarmonyOs(){
    try {
        Class cls = Class.forName("ohos.utils.system.SystemCapability");
        return cls!=null;
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        return false;
    }
}
复制代码

通过在设置页面开启/关闭相关app的平行视界

// 平行视界开启:
2021-07-06 10:02:59.573 1644-1847/? D/HWMW_HwMagicWindowManagerService: setHwMagicWinEnabled, pkg = cn.firstleap.parent, enabled = true
2021-07-06 10:02:59.573 1644-1847/? D/HWMW_HwMagicWindowConfig: onAppSwitchChanged, pkg = cn.firstleap.parent, hwMagicWinEnabled = true

// 平行视界关闭:
2021-07-06 10:03:35.929 1644-1848/? D/HWMW_HwMagicWindowManagerService: setHwMagicWinEnabled, pkg = cn.firstleap.parent, enabled = false
2021-07-06 10:03:35.929 1644-1848/? D/HWMW_HwMagicWindowConfig: onAppSwitchChanged, pkg = cn.firstleap.parent, hwMagicWinEnabled = false
复制代码

目前无法找到这个api,放弃;

打印Configuration显示信息:

// 平行视界关闭:Configuration内容:
{1.0 ?mcc?mnc [zh_CN_#Hans] ldltr sw640dp w1024dp h608dp 400dpi lrg hdr land finger -keyb/v/h -nav/h winConfig={ mBounds=Rect(0, 0 - 2560, 1600) mAppBounds=Rect(0, 0 - 2560, 1600) mWindowingMode=fullscreen mDisplayWindowingMode=fullscreen mActivityType=standard mAlwaysOnTop=undefined mRotation=ROTATION_90} suim:1 fontWeightScale:100 s.3}

// 平行视界开启:Configuration内容:
{1.0 ?mcc?mnc [zh_CN_#Hans] ldltr sw508dp w508dp h640dp 400dpi lrg hdr port finger -keyb/v/h -nav/h winConfig={ mBounds=Rect(644, 0 - 1916, 1600) mAppBounds=Rect(644, 0 - 1916, 1600) mWindowingMode=hw-magic-windows mDisplayWindowingMode=fullscreen mActivityType=standard mAlwaysOnTop=undefined mRotation=ROTATION_0} suim:1 fontWeightScale:100 s.6}
复制代码

可以分析到:mWindowingMode为fullscreen正常情况;为hw-magic-windows平行视界开启了。
代码判断:

/**
 *
 * 平行视界是否开启
 *
 */
public static boolean isHWMagicWindow(Context context) {
    boolean hwMagicWindows = false;
    try {
        Resources res = context.getResources();
        if (res != null) {
            Configuration config = res.getConfiguration();
            if (config != null) {
                String con = config.toString();
                if (con.contains("hw-magic-windows")) {
                    hwMagicWindows = true;
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    Log.d(TAG, "hwMagicWindows:" + hwMagicWindows);
    return hwMagicWindows;
}
复制代码

经过测试:第一次如果进入的是竖屏ui显示正常,返回false;进入横屏ui显示异常,返回为true;再进入竖屏页面发现显示异常,返回为false。

可以得出结论:这种方式一般适用于对于异常设备提示。
还是建议进行适配,但是适配工作量有点大(特别是没有鸿蒙开发api经验的)需要熟悉鸿蒙api和相关文档。

鸿蒙相关文档:
平行视界专题
多窗口交互-平行视界
鸿蒙api

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享