#### 1、HarmonyOS 在自由多窗模式下,有无监测获取窗口大小的方法,获取窗口的宽度和高度?
监听应用窗口变化参考地址:
https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-arkui/js-apis-window.md#onwindowstatuschange11


#### 2、HarmonyOS 自定义键盘防录屏方案?
项目开发中使用了自定义键盘,目前有个安全合规需求,需要在屏幕录制的时候能够实现自定义键盘(或指定某些UI组件)不被录制到视频中去,目前是否提供了防止录屏的方案呢?

目前没有针对组件防止录屏的方案,只能在特殊窗口通过设置隐私模式来防止录屏截屏。该配置是针对当前窗口的,参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-window-V5#setwindowprivacymode9
`setWindowPrivacyMode(isPrivacyMode: boolean, callback: AsyncCallback<void>): void`
设置窗口是否为隐私模式,使用callback异步回调。设置为隐私模式的窗口,窗口内容将无法被截屏或录屏。此接口可用于禁止截屏/录屏的场景。

|参数名|类型|必填|说明|
|:--|:--|:--|:--|
|isPrivacyMode|boolean|是|窗口是否为隐私模式。true表示模式开启;false表示模式关闭。|
|callback|AsyncCallback<void>|是|回调函数。|


#### 3、HarmonyOS 折叠屏折叠状态切换时屏幕宽度获取不对?
foldstatus监听,就是通知折叠状态变化,折叠开合过程中超过半折态的阈值就会更新foldstatus,折叠开合改变了foldstatus以后还要去通知屏幕发生变化,更新屏幕数据,foldstatus的状态变化没有问题,屏幕处理折叠开合事件以后更新屏幕宽高,更新折叠屏显示模式;

display.on(foldstatus)通知的是foldstatus的变化,这个时候去查foldstatus是实时更新的,折叠开合流程还没结束,读到的属性是旧值没有意义display.on(change)监听屏幕属性的变化,这个监听也是屏幕属性更新完触发,但这个不区分什么原因导致的屏幕属性变化display.on(folddisplaymode)监听屏幕显示模式变化,是折叠开合显示模式切换过来以后再收到通知,收到这个回调去读屏幕属性是折叠开合流程结束的数据


#### 4、HarmonyOS 在MatePad设备上如何开启自由多窗模式?
参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/multi-faq-V5#%E5%A6%82%E4%BD%95%E5%BC%80%E5%90%AF%E8%87%AA%E7%94%B1%E7%AA%97%E5%8F%A3

自由窗口功能默认是关闭的,可以通过如下方式开启自由窗口功能。
```
# 取出窗口配置文件,并将文件中的<decor enable="false"></decor>修改为<decor enable="true"></decor>
hdc file recv system/etc/window/resources/window_manager_config.xml ./
# 以可读写的模式重新挂载根目录,并更新配置文件
hdc shell mount -o rw,remount /
hdc file send window_manager_config.xml system/etc/window/resources/window_manager_config.xml
# 重启设备,配置生效
hdc shell reboot
```

屏幕较小,通过手指操作窗口较为不便时,建议外接鼠标进行操作。

- 鼠标在应用顶部悬停,即可召唤出窗口工具栏。
- 点击窗口工具栏中的缩放按钮(从左到右第二个),即可让应用以自由窗口的模式显示。
- 在自由窗口模式下,可以通过拖动应用窗口的边框或顶角,改变窗口尺寸同时触发应用显示刷新。
在调整窗口尺寸的过程中,窗口尺寸可能超出屏幕尺寸。此时应用显示正常,但受限于屏幕尺寸,在屏幕中只能看到应用部分区域的显示。可以通过移动窗口位置,查看应用其它区域的显示。

#### 5、HarmonyOS APP内悬浮按钮开发方案?
App内需要一个在app内部页面间悬浮的控件(不申请系统悬浮窗权限),用于点击后快速回到指定位置,这个功能在友商是在baseActivity实现给每个页面添加悬浮控件,达到页面间悬浮假象。我看HarmonyOS文档中关于悬浮窗功能的实现,发现都不太适合这个功能,如果要像友商那样在每个页面添加悬浮控件,因为页面间没有继承关系,会很复杂。请问,这种在HarmonyOS下如何实现比较好?

可以使用createSubWindow设置子窗口以实现悬浮窗的功能参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/application-window-stage-V5#%E8%AE%BE%E7%BD%AE%E5%BA%94%E7%94%A8%E5%AD%90%E7%AA%97%E5%8F%A3

1. 创建应用子窗口。
    通过createSubWindow接口创建应用子窗口。
2. 设置子窗口属性。
    子窗口创建成功后,可以改变其大小、位置等,还可以根据应用需要设置窗口背景色、亮度等属性。
3. 加载显示子窗口的具体内容。
    通过setUIContent和showWindow接口加载显示子窗口的具体内容。
4. 销毁子窗口。
    当不再需要某些子窗口时,可根据具体实现逻辑,使用destroyWindow接口销毁子窗口。
    
直接在onWindowStageCreate里面创建子窗口的整体示例代码如下:
```
import { UIAbility } from '@kit.AbilityKit';
import { window } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';


let windowStage_: window.WindowStage | null = null;
let sub_windowClass: window.Window | null = null;


export default class EntryAbility extends UIAbility {
  showSubWindow() {
    // 1.创建应用子窗口。
    if (windowStage_ == null) {
      console.error('Failed to create the subwindow. Cause: windowStage_ is null');
    }
    else {
      windowStage_.createSubWindow("mySubWindow", (err: BusinessError, data) => {
        let errCode: number = err.code;
        if (errCode) {
          console.error('Failed to create the subwindow. Cause: ' + JSON.stringify(err));
          return;
        }
        sub_windowClass = data;
        console.info('Succeeded in creating the subwindow. Data: ' + JSON.stringify(data));
        // 2.子窗口创建成功后,设置子窗口的位置、大小及相关属性等。
        sub_windowClass.moveWindowTo(300, 300, (err: BusinessError) => {
          let errCode: number = err.code;
          if (errCode) {
            console.error('Failed to move the window. Cause:' + JSON.stringify(err));
            return;
          }
          console.info('Succeeded in moving the window.');
        });
        sub_windowClass.resize(500, 500, (err: BusinessError) => {
          let errCode: number = err.code;
          if (errCode) {
            console.error('Failed to change the window size. Cause:' + JSON.stringify(err));
            return;
          }
          console.info('Succeeded in changing the window size.');
        });
        // 3.为子窗口加载对应的目标页面。
        sub_windowClass.setUIContent("pages/page3", (err: BusinessError) => {
          let errCode: number = err.code;
          if (errCode) {
            console.error('Failed to load the content. Cause:' + JSON.stringify(err));
            return;
          }
          console.info('Succeeded in loading the content.');
          // 3.显示子窗口。
          (sub_windowClass as window.Window).showWindow((err: BusinessError) => {
            let errCode: number = err.code;
            if (errCode) {
              console.error('Failed to show the window. Cause: ' + JSON.stringify(err));
              return;
            }
            console.info('Succeeded in showing the window.');
          });
        });
      })
    }
  }


  destroySubWindow() {
    // 4.销毁子窗口。当不再需要子窗口时,可根据具体实现逻辑,使用destroy对其进行销毁。
    (sub_windowClass as window.Window).destroyWindow((err: BusinessError) => {
      let errCode: number = err.code;
      if (errCode) {
        console.error('Failed to destroy the window. Cause: ' + JSON.stringify(err));
        return;
      }
      console.info('Succeeded in destroying the window.');
    });
  }


  onWindowStageCreate(windowStage: window.WindowStage) {
    windowStage_ = windowStage;
    // 开发者可以在适当的时机,如主窗口上按钮点击事件等,创建子窗口。并不一定需要在onWindowStageCreate调用,这里仅作展示
    this.showSubWindow();
  }


  onWindowStageDestroy() {
    // 开发者可以在适当的时机,如子窗口上点击关闭按钮等,销毁子窗口。并不一定需要在onWindowStageDestroy调用,这里仅作展示
    this.destroySubWindow();
  }
};
```

 

Logo

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

更多推荐