HarmonyOS 应用全部页面防截屏,无法截屏/录屏

HarmonyOS
2024-12-18 16:34:42
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

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

参考代码:

//module.json5文件中
{
  "name" : 'ohos.permission.PRIVACY_WINDOW'
}

//EAbility文件中

onWindowStageCreate(windowStage: window.WindowStage): void {
  let windowClass: window.Window = windowStage.getMainWindowSync();
  // 获取应用主窗口

  AppStorage.setOrCreate("windowClass",windowClass);
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
//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.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
分享
微博
QQ
微信
回复
2024-12-18 18:26:37


相关问题
HarmonyOS 如何
1056浏览 • 1回复 待解决
页面如何
335浏览 • 0回复 待解决
flutter的页面如何
432浏览 • 1回复 待解决
如何、投
3436浏览 • 1回复 待解决
HarmonyOS
999浏览 • 1回复 待解决
HarmonyOS 如何禁用
1055浏览 • 1回复 待解决
HarmonyOS 单个页面问题
594浏览 • 1回复 待解决
防止屏幕或者
1482浏览 • 1回复 待解决
HarmonyOS 如何设置放
1296浏览 • 1回复 待解决
HarmonyOS 如何禁止
1533浏览 • 1回复 待解决
HarmonyOS 不被或者如何实现
734浏览 • 1回复 待解决
HarmonyOS 密码登录页面未做
1134浏览 • 1回复 待解决
HarmonyOS 功能如何实现
926浏览 • 1回复 待解决
HarmonyOS
819浏览 • 1回复 待解决
求告知如何实现功能
1175浏览 • 1回复 待解决
求大佬告知如何实现功能
2772浏览 • 1回复 待解决