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

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%')
  }
}
相关推荐
Swift社区2 小时前
鸿蒙游戏里的 AI Agent 设计
人工智能·游戏·harmonyos
亚历克斯神2 小时前
Flutter 组件 t_stats 的适配 鸿蒙Harmony 实战 - 驾驭高性能统计学运算、实现鸿蒙端海量数据实时态势感知与工业级描述性统计方案
flutter·harmonyos·鸿蒙·openharmony·t_stats
键盘鼓手苏苏2 小时前
Flutter 组件 angel3_orm_mysql 的适配 鸿蒙Harmony 实战 - 驾驭专业 ORM 映射引擎、实现鸿蒙端与 MySQL 数据库的透明映射与高性能 SQL 审计方案
flutter·harmonyos·鸿蒙·openharmony·angel3_orm_mysql
左手厨刀右手茼蒿2 小时前
Flutter 组件 serverpod_swagger 的适配 鸿蒙Harmony 实战 - 驾驭 API 文档自动化、实现鸿蒙端全栈联调与 Swagger UI 动态审计方案
flutter·harmonyos·鸿蒙·openharmony·serverpod_swagger
钛态2 小时前
Flutter 三方库 discord_interactions 的鸿蒙化适配指南 - 在 OpenHarmony 打造高效的社交机器人交互底座
flutter·harmonyos·鸿蒙·openharmony·discord_interactions
加农炮手Jinx2 小时前
Flutter 组件 dascade 的适配 鸿蒙Harmony 实战 - 驾驭级联式异步数据流、实现鸿蒙端响应式 UI 状态泵与复杂业务逻辑解耦方案
flutter·harmonyos·鸿蒙·openharmony
国医中兴2 小时前
Flutter 组件 r_flutter 的适配 鸿蒙Harmony 实战 - 驾驭资源映射自动化、实现鸿蒙端资产强类型引用与资产冲突静态校验方案
flutter·r语言·harmonyos
里欧跑得慢2 小时前
Flutter 组件 postgres_crdt 的适配 鸿蒙Harmony 实战 - 驾驭分布式无冲突复制数据类型、实现鸿蒙端高性能离线对等同步架构方案
flutter·harmonyos·鸿蒙·openharmony·postgres_crdt
王码码20352 小时前
Flutter for OpenHarmony:使用 pluto_grid 打造高性能数据网格
flutter·http·华为·架构·harmonyos
王码码20352 小时前
Flutter 组件 d_method 适配 鸿蒙Harmony 实战 - 驾驭核心极地盲区开发终端调试网络、构建极简跨层穿透护级大日志脱机雷达防线
flutter·harmonyos·鸿蒙·openharmony·d_method