鸿蒙开发中如何实现防截屏功能?

鸿蒙开发中如何实现防截屏功能?

阅读 959
1 个回答

可以使用 setWindowPrivacyMode(isPrivacyMode: boolean, callback: AsyncCallback<void>): void 来设置窗口是否为隐私模式,使用 callback 异步回调。设置为隐私模式的窗口,窗口内容将无法被截屏或录屏。此接口可用于禁止截屏/录屏的场景。
需要权限:ohos.permission.PRIVACY_WINDOW
参考代码如下:

import { BusinessError } from '@ohos.base';  
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));  
}

实现步骤:

  • 设置窗口为隐私模式时,需要在 entry 的 module.json5 文件中添加相关权限。
  • 参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-refere...
  • 对于 Page 的场景,实现步骤为:首先需要在 module.json5 中申请隐私窗口权限。然后在 onPageShow()中获取当前窗口对象并设置隐私模式。在退出页面的时候,在 onPageHide()生命周期中取消隐私模式即可。
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进