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

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%')
  }
}
相关推荐
GitCode官方2 小时前
直播预约|开源鸿蒙PC命令行工具迁移实战:从环境搭建到真机验证全流程拆解
人工智能·华为·开源·harmonyos·atomgit
lqj_本人2 小时前
鸿蒙electron跨端框架PC工志簿实战:项目、工时、阻塞和下一步都要有位置
数据库·华为·harmonyos
胡琦博客7 小时前
Tauri 如何跑到鸿蒙上?从 tauri-demo 看 OpenHarmony 适配链路
华为·harmonyos
nashane7 小时前
HarmonyOS 6学习:文件打开方式应用重复的根治方案与最佳实践
学习·华为·harmonyos
Swift社区7 小时前
AI + 鸿蒙 App:下一代应用架构
人工智能·架构·harmonyos
ZHW_AI课题组8 小时前
调用华为云API实现图像标签识别
图像处理·华为·华为云
枫叶丹48 小时前
【HarmonyOS 6.0】Enterprise Space Kit:空间管理服务深入解析
开发语言·华为·harmonyos
lqj_本人9 小时前
鸿蒙PC:marktext-develop鸿蒙适配全记录
华为·harmonyos
Python私教9 小时前
鸿蒙 Agent Framework Kit:FunctionComponent 把智能体嵌进 ArkTS 页面
华为·harmonyos