鸿蒙next之如何实现防截屏功能

在HarmonyOS next中实现防截屏主要有两种方式

setWindowPrivacyMode设置窗口是否为隐私模式,设置为隐私模式的窗口,窗口内容将无法被截屏或录屏。此接口可用于禁止截屏/录屏的场景。

方式一: 在onWindowStageCreate回调中设置主窗口为隐私模式,具体可参考示例代码:

js 复制代码
import { window } from '@kit.ArkUI'; 
import { BusinessError } from '@kit.BasicServicesKit';
 onWindowStageCreate(windowStage: window.WindowStage): void { 
    // Main window is created, set main page for this ability 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); 
    // 获取主窗口 
    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; 
      } 
      let windowClass: window.Window = data; 
      console.info('Succeeded in obtaining the main window. Data: ' + JSON.stringify(data)); 
      // 设置窗口隐私模式 
      let isPrivacyMode: boolean = true; 
      try { 
        windowClass.setWindowPrivacyMode(isPrivacyMode, (err: BusinessError) => { 
          const errCode: number = err.code; 
          if (errCode) { 
            console.error('Failed to set the window to privacy mode. Cause:' + JSON.stringify(err)); 
            return; 
          } 
          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)); 
      } 
    }) 
    windowStage.loadContent('pages/Index', (err, data) => { 
      if (err.code) { 
        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); 
        return; 
      } 
      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? ''); 
    }); 
  }

方式二: 进入页面开启隐式模式,离开页面取消,具体可参考以下步骤:

首先在module.json5文件中声明需要使用的ohos.permission.PRIVACY_WINDOW 权限

js 复制代码
"requestPermissions": [
  {"name": "ohos.permission.PRIVACY_WINDOW"}
]

然后在进入页面时触发onPageShow回调,调用setWindowPrivacyMode设置窗口为隐私模式,离开页面时触发onPageHide回调,设置窗口为非隐私模式,参考示例代码如下:

js 复制代码
import { BusinessError } from '@kit.BasicServicesKit';
import { common } from '@kit.AbilityKit';
import { window } from '@kit.ArkUI';

class windowUtils {
  static setWindowPrivacyModeInPage(context: common.UIAbilityContext, isFlag: boolean) {
    window.getLastWindow(context).then((lastWindow) => {
      lastWindow.setWindowPrivacyMode(isFlag, (err: BusinessError) => {
        const errCode: number = err.code;
        if (errCode) {
          console.error('Failed to set the window to privacy mode. 1Cause:' + JSON.stringify(err));
          return;
        }
        console.info('Succeeded in setting the window to privacy mode.');
      });
    })
  }
}

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';

  onPageShow() {
    windowUtils.setWindowPrivacyModeInPage(getContext(this) as common.UIAbilityContext, true);
  }

  onPageHide() {
    windowUtils.setWindowPrivacyModeInPage(getContext(this) as common.UIAbilityContext, false);
  }

  build() {
    RelativeContainer() {
      Text(this.message)
        .id('HelloWorld')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
        .alignRules({
          center: { anchor: '__container__', align: VerticalAlign.Center },
          middle: { anchor: '__container__', align: HorizontalAlign.Center }
        })
    }
    .height('100%')
    .width('100%')
  }
}
相关推荐
安卓开发者14 小时前
鸿蒙NEXT应用接入快捷栏:一键直达,提升用户体验
java·harmonyos·ux
HMS Core14 小时前
消息推送策略:如何在营销与用户体验间找到最佳平衡点
华为·harmonyos·ux
Brianna Home14 小时前
【案例实战】鸿蒙分布式调度:跨设备协同实战
华为·wpf·harmonyos
Bert丶seven14 小时前
鸿蒙Harmony实战开发教学(No.4)-RichText组件基础到高阶介绍篇
华为·harmonyos·arkts·鸿蒙·鸿蒙系统·arkui·开发教程
鸿蒙小白龙15 小时前
openharmony之分布式蓝牙实现多功能场景设备协同实战
分布式·harmonyos·鸿蒙·鸿蒙系统·open harmony
爱吃水蜜桃的奥特曼16 小时前
玩Android Harmony next版,通过项目了解harmony项目快速搭建开发
android·harmonyos
鸿蒙小白龙17 小时前
openharmony之分布式购物车开发实战
分布式·harmonyos·鸿蒙·鸿蒙系统·open harmony
鸿蒙小白龙17 小时前
openharmony之分布式相机开发:预览\拍照\编辑\同步\删除\分享教程
分布式·harmonyos·鸿蒙·鸿蒙系统·open harmony
安卓开发者20 小时前
鸿蒙NEXT鼠标光标开发完全指南
华为·计算机外设·harmonyos
●VON1 天前
重生之我在大学自学鸿蒙开发第九天-《分布式流转》
学习·华为·云原生·harmonyos·鸿蒙