HarmonyOS 是否有防录屏的功能

HarmonyOS
2024-12-25 12:25:06
1096浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

一些页面需要实现防截屏功能。首先需要在module.json5中申请隐私窗口权限。 然后在onPageShow()中获取当前窗口对象并设置隐私模式。 在退出页面的时候,在onPageHide()生命周期中取消隐私模式即可,参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-window-V5#setwindowprivacymode9

示例如下:

//module.json5文件中
{
  "name": 'ohos.permission.PRIVACY_WINDOW'
}
  • 1.
  • 2.
  • 3.
  • 4.
//EAbility文件中
onWindowStageCreate(windowStage: window.WindowStage): void {
  let windowClass: window.Window = windowStage.getMainWindowSync(); // 获取应用主窗口
  AppStorage.setOrCreate("windowClass", windowClass);
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
//Index文件中
import { window } from '@kit.ArkUI';
import { BusinessError } from '@ohos.base';

@Entry
@Component
struct Index43 {
  private windowStage = AppStorage.get("windowStage") as window.WindowStage
  private windowClass = AppStorage.get("windowClass") as window.Window
  @State message: string = 'Hello World';

  build() {
    Row() {
      Column() {
        Text('禁止截屏').fontSize(50).fontWeight(FontWeight.Bold).onClick(() => {
          let isPrivacyMode: boolean = true;
          try {
            this.windowClass.setWindowPrivacyMode(isPrivacyMode, (err: BusinessError) => {
              const errCode: number = err.code;
              if (errCode) {
                console.error('Failed to set the window to privacy mode. Cause:' + JSON.stringify(err));
                return;
              }
              console.info('Succeeded in setting the window to privacy mode.');
            });
          } catch (exception) {
            console.error('Failed to set the window to privacy mode. Cause:' + JSON.stringify(exception));
          }
        })
        Text('允许截屏').fontSize(50).fontWeight(FontWeight.Bold).onClick(() => {
          let isPrivacyMode: boolean = false;
          try {
            this.windowClass.setWindowPrivacyMode(isPrivacyMode, (err: BusinessError) => {
              const errCode: number = err.code;
              if (errCode) {
                console.error('Failed to set the window to privacy mode. Cause:' + JSON.stringify(err));
                return;
              }
              console.info('Succeeded in setting the window to privacy mode.');
            });
          } catch (exception) {
            console.error('Failed to set the window to privacy mode. Cause:' + JSON.stringify(exception));
          }
        })
      }.width('100%')
    }.height('100%')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
分享
微博
QQ
微信
回复
2024-12-25 15:31:48
相关问题
HarmonyOS 如何
1007浏览 • 1回复 待解决
HarmonyOS
767浏览 • 1回复 待解决
HarmonyOS是否API
1065浏览 • 1回复 待解决
页面如何和截
246浏览 • 0回复 待解决
flutter页面如何和截
351浏览 • 1回复 待解决
HarmonyOS 是否监听api
605浏览 • 1回复 待解决
HarmonyOS 自定义键盘方案
744浏览 • 1回复 待解决
HarmonyOS 功能如何实现
859浏览 • 1回复 待解决
求告知如何实现功能
1122浏览 • 1回复 待解决
请问API有没有功能
9785浏览 • 1回复 待解决
求大佬告知如何实现功能
2721浏览 • 1回复 待解决
HarmonyOS
935浏览 • 1回复 待解决
如何实现功能,有人知道吗?
2686浏览 • 1回复 待解决
HarmonyOS 是否屏幕共享能力
861浏览 • 1回复 待解决
如何截、投
3263浏览 • 1回复 待解决
HarmonyOS 如何禁用截
998浏览 • 1回复 待解决
6月8号直播课吗?
6382浏览 • 2回复 待解决