HarmonyOS 如何防截屏防录屏

HarmonyOS
2024-12-25 07:55:38
1096浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Heiang

一些页面需要实现防截屏功能。首先需要在module.json5中申请隐私窗口权限。

然后在onPageShow()中获取当前窗口对象并设置隐私模式。

在退出页面的时候,在onPageHide()生命周期中取消隐私模式即可。弹窗从屏幕底部向上弹出展示的动画如何实现?

参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-window-V5#setwindowprivacymode9

参考demo如下:

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

//EAbility文件中

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

  AppStorage.setOrCreate("windowClass",windowClass);
}


//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.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
分享
微博
QQ
微信
回复
2024-12-25 09:48:31


相关问题
页面如何
249浏览 • 0回复 待解决
HarmonyOS
767浏览 • 1回复 待解决
flutter的页面如何
353浏览 • 1回复 待解决
HarmonyOS
935浏览 • 1回复 待解决
HarmonyOS 功能如何实现
863浏览 • 1回复 待解决
HarmonyOS 单个页面问题
533浏览 • 1回复 待解决
HarmonyOS 是否有的功能
894浏览 • 1回复 待解决
求告知如何实现功能
1122浏览 • 1回复 待解决
求大佬告知如何实现功能
2723浏览 • 1回复 待解决
HarmonyOS 自定义键盘方案
744浏览 • 1回复 待解决
HarmonyOS 密码登录页面未做
1085浏览 • 1回复 待解决
如何实现功能,有人知道吗?
2686浏览 • 1回复 待解决
如何、投
3263浏览 • 1回复 待解决
HarmonyOS 如何禁用
1003浏览 • 1回复 待解决
HarmonyOS 如何设置放
1245浏览 • 1回复 待解决
HarmonyOS 如何禁止
1429浏览 • 1回复 待解决
HarmonyOS 不被或者如何实现
694浏览 • 1回复 待解决
防止屏幕或者
1435浏览 • 1回复 待解决