HarmonyOS 口令输入界面可以被截屏或者录屏怎么处理

HarmonyOS
2025-01-09 17:03:46
351浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

在EntryAbility的onWindowStageCreate中缓存:

let windowClass: window.Window = windowStage.getMainWindowSync()
// 获取应用主窗口
AppStorage.setOrCreate("windowClass", windowClass)
  • 1.
  • 2.
  • 3.

使用

import { window } from '@kit.ArkUI';
import { BusinessError } from '@ohos.base';

@Entry
@Component
struct Index43 {
  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.
import { window } from '@kit.ArkUI';
import { BusinessError } from '@ohos.base';

@Entry
@Component
struct Index43 {
  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.
import { window } from '@kit.ArkUI';
import { BusinessError } from '@ohos.base';

@Entry
@Component
struct Index43 {
  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.

开启和关闭写在页面对应的生命周期中。

分享
微博
QQ
微信
回复
2025-01-09 19:15:06


相关问题
防止屏幕或者
1278浏览 • 1回复 待解决
HarmonyOS 不被或者如何实现
563浏览 • 1回复 待解决
如何、投
3013浏览 • 1回复 待解决
HarmonyOS 如何禁用
858浏览 • 1回复 待解决
HarmonyOS 如何防
864浏览 • 1回复 待解决
HarmonyOS 如何设置放
1091浏览 • 1回复 待解决
HarmonyOS 如何禁止
1221浏览 • 1回复 待解决
页面如何防
29浏览 • 0回复 待解决
flutter的页面如何防
133浏览 • 1回复 待解决
HarmonyOS 安全键盘输入问题
741浏览 • 1回复 待解决
dayu200开发板如何或者
846浏览 • 0回复 待解决
恭喜您,今日已阅读两篇内容,特奖励+2声望, 快来领取吧。