单页面防截屏鸿蒙示例代码

鸿蒙场景化示例代码技术工程师
发布于 2025-4-9 16:03
浏览
0收藏

本文原创发布在华为开发者社区

介绍

本示例基于原生能力,实现对单个页面设置为隐私模式,使其可以禁止截屏,录屏及分享屏幕等行为。

单页面防截屏源码链接

效果预览

单页面防截屏鸿蒙示例代码-鸿蒙开发者社区

实现思路

  1. onWindowStageCreate生命周期函数中获取windowClass对象并保存,在对应需要防截屏的页面获取对象并设置隐私模式:
 windowStage.getMainWindow((err: BusinessError, data) => {
  let errCode: number = err.code;
  if (errCode) {
  console.error('Failed to obtain the main window. Cause: ' + JSON.stringify(err));
  return;
  }
  const windowClass = data;
  console.info('Succeeded in obtaining the main window. Data: ' + JSON.stringify(data));
  AppStorage.setOrCreate("windowClass",windowClass)
})
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  1. 需要设置防截屏页面:
@State windowClass: window.Window | null | undefined = AppStorage.get('windowClass')
aboutToAppear(): void {
    let isPrivacyMode: boolean = true
    if (this.windowClass) {
      this.windowClass.setWindowPrivacyMode(isPrivacyMode); // 设置防截屏录屏
    }
  }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

分类
收藏
回复
举报
回复
    相关推荐