鸿蒙中开启关闭防截屏录屏

1.申请权限

TypeScript 复制代码
  "requestPermissions": [
      {
        "name": "ohos.permission.PRIVACY_WINDOW"
      },
    ],

2.ui

TypeScript 复制代码
import { common, OpenLinkOptions, Want } from '@kit.AbilityKit'
import { BusinessError } from '@kit.BasicServicesKit'
import { window } from '@kit.ArkUI';
import { promptAction } from '@kit.ArkUI';

@Entry()
@Component
struct TransactionHistory {
  async setWindowPrivacyModeTrue(context: Context) {
    let windowClass: window.Window = await window.getLastWindow(context)
    try {
      windowClass.setWindowPrivacyMode(true, (err: BusinessError) => {
        const errCode: number = err.code;
        if (errCode) {
          console.error('Failed to set the window to privacy mode. Cause:' + JSON.stringify(err));
          if (errCode == 201) {

          }
          return;
        }
        promptAction.showToast({
          message: `已开启 防截屏录屏`,
          duration: 2000,
          bottom: '500lpx'
        });
        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));
    }
  }

  async setWindowPrivacyModeFalse(context: Context) {
    let windowClass: window.Window = await window.getLastWindow(context)
    try {
      windowClass.setWindowPrivacyMode(false, (err: BusinessError) => {
        const errCode: number = err.code;
        if (errCode) {
          console.error('Failed to set the window to privacy mode. Cause:' + JSON.stringify(err));
          if (errCode == 201) {

          }
          return;
        }
        promptAction.showToast({
          message: `已关闭 防截屏录屏`,
          duration: 2000,
          bottom: '500lpx'
        });
        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));
    }
  }

  build() {
    Column() {
      Button('开启防截屏录屏').onClick(() => {
        this.setWindowPrivacyModeTrue(getContext())
      })
      Button('关闭防截屏录屏').onClick(() => {
        this.setWindowPrivacyModeFalse(getContext())
      })
    }.width('100%')
  }
}
相关推荐
一只栖枝1 分钟前
华为 HCIE 大数据认证中 Linux 命令行的运用及价值
大数据·linux·运维·华为·华为认证·hcie·it
zhanshuo4 小时前
在鸿蒙里优雅地处理网络错误:从 Demo 到实战案例
harmonyos
zhanshuo4 小时前
在鸿蒙中实现深色/浅色模式切换:从原理到可运行 Demo
harmonyos
whysqwhw9 小时前
鸿蒙分布式投屏
harmonyos
whysqwhw11 小时前
鸿蒙AVSession Kit
harmonyos
whysqwhw12 小时前
鸿蒙各种生命周期
harmonyos
whysqwhw13 小时前
鸿蒙音频编码
harmonyos
whysqwhw13 小时前
鸿蒙音频解码
harmonyos
whysqwhw13 小时前
鸿蒙视频解码
harmonyos
whysqwhw14 小时前
鸿蒙视频编码
harmonyos