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

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%')
  }
}
相关推荐
颜颜yan_16 分钟前
ESP-IDF 鸿蒙 PC 适配全记录:打通 Python、构建工具链与 ESP32-P4 固件生成
python·华为·harmonyos
梦想不只是梦与想3 小时前
鸿蒙 邀请测试:AppGallery邀请测试流程
harmonyos·appgallery 邀请测试·邀请测试
贾伟康6 小时前
【口算王|02】HarmonyOS ArkTS 答题提交实战:防止重复提交并推进下一题
harmonyos·arkts·状态管理·arkui·幂等设计
承渊政道8 小时前
Python IDLE鸿蒙PC适配全记录:从Tkinter桌面程序到ArkUI原生开发闭环
开发语言·python·harmonyos·鸿蒙系统·桌面程序
万物智能信息科技8 小时前
电容触摸 FT5406,另一颗芯片、同一条总线—【万物智能之开源鸿蒙OpenHarmony系统实战开发系列教程】
人工智能·华为·开源·harmonyos·鸿蒙
ChinaDragonDreamer8 小时前
HarmonyOS:应用程序包管理模块(获取当前应用信息)
harmonyos·鸿蒙
●VON8 小时前
Flutter 鸿蒙插件适配实战:用 boot_time_plugin 1.0.0 读取启动时间与运行时长
flutter·华为·harmonyos
颜颜yan_9 小时前
Git Cola 鸿蒙 PC 版适配全记录:从 Qt 桌面工具到 ArkTS 原生应用
git·qt·harmonyos
承渊政道10 小时前
Python IDLE鸿蒙PC适配全记录:用 ArkUI 重建编辑、运行、Shell 与基础调试闭环
python·microsoft·harmonyos·鸿蒙系统·pc端
User_芊芊君子10 小时前
RapidSVN 鸿蒙 PC 适配全记录:用 ArkUI 重建工作台,打通本地 SVN 操作闭环
华为·svn·harmonyos