实现页面隐私模式鸿蒙示例代码

鸿蒙场景化示例代码技术工程师
发布于 2025-3-18 15:36
浏览
0收藏

本文原创发布在华为开发者社区

介绍

本示例基于setWindowPrivacyMode接口实现了将页面设置为禁止截屏或录屏的隐私模式。

实现页面隐私模式源码链接

效果预览

实现页面隐私模式鸿蒙示例代码-鸿蒙开发者社区

使用说明

点击“开启隐藏”按钮,进入隐私模式,截屏时会出现弹窗提示,禁止截屏,录屏时会隐藏页面。点击“关闭隐藏”按钮,将退出隐私模式。

实现思路

通过setWindowPrivacyMode接口设置窗口是否为隐私模式,使用callback异步回调,设置为隐私模式的窗口,窗口内容将无法被截屏或录屏。核心代码如下,源码参考Index.ets。

Button() {
          Text('开启隐藏');
        }
        .width(80)
        .height(60)
        .onClick(() => {
          let isPrivacyMode: boolean = true;
          try {
            window.getLastWindow(getContext(), (err: BusinessError, data) => {
              const errCode = err.code;
              if (errCode) {
                return;
              }
              let promise = data.setWindowPrivacyMode(isPrivacyMode);
              promise.then(() => {
                this.message = '隐私模式';
                hilog.info(0x0000, 'testTag', '已成功将窗口设置为隐私模式.');
              }).catch((err: BusinessError) => {
                hilog.error(0x0000, 'testTag', 'Failed to set the window to privacy mode. Cause: ' + JSON.stringify(err));
              });
            });
          } catch (exception) {
            hilog.error(0x0000, 'testTag', 'Failed to set the window to privacy mode. Cause:' + JSON.stringify(exception));
          }
        });
  • 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.

分类
收藏
回复
举报
回复
    相关推荐