HarmonyOS 设置防截屏录屏?

在登录页面怎么设置防截屏录屏

阅读 514
1 个回答

防止录频和截屏的方案:

1.在module.json5添加ohos.permission.PRIVACY\_WINDOW权限

2.在EntryAbility文件中添加下面的代码:

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

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

3.在所在页面添加代码:

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
  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%')
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进