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

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%')
  }
}
相关推荐
威哥爱编程7 小时前
HarmonyOS 7 自由多窗实战:supportWindowMode + 断点布局,大屏多任务主动接管
harmonyos·arkts
威哥爱编程7 小时前
HarmonyOS 7 应用接续实战:continuable + onContinue 跨设备无缝流转,手机编辑平板接着搞
harmonyos·arkts
威哥爱编程7 小时前
HarmonyOS 7 视觉 AI 进阶实战:人脸检测 + 通用文字识别(OCR)两步接入
harmonyos
李游Leo14 小时前
HarmonyOS 7 API 26 升级适配实战:API Change Assistant、targetSDKVersion 与兼容性回归
harmonyos
OH_TPC14 小时前
HarmonyOS APP开发---"智泊"智能停车App,需要用到这个库
harmonyos
用户1179104883315 小时前
AI粘贴多行需求为什么会误执行?hmharness安全粘贴拆解 GitHub:swsgbl/hmharness
ai编程·harmonyos
用户09340777351415 小时前
HarmonyOS WPS Open SDK 实践:从 HAR 集成到 OpenFileRequest 最小闭环
typescript·harmonyos
威哥爱编程1 天前
HarmonyOS 7 Fast Kit 算法加速实战:四大核心能力,把算力从系统里"借"出来
harmonyos·arkts
威哥爱编程1 天前
HarmonyOS 7 游戏快启实战:Graphics Accelerate Kit 内存镜像秒级启动 + 预启动,把读条变成"秒进"
harmonyos·arkts
木子雨廷1 天前
第 19 天|Preferences:轻量缓存与状态恢复
harmonyos