实现单页面防截屏功能鸿蒙示例代码
介绍
本示例基于原生能力,实现对单个页面设置为隐私模式,使其可以禁止截屏,录屏及分享屏幕等行为。
效果预览

使用说明
实现思路
- 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)
})
- 需要设置防截屏页面:
@State windowClass: window.Window | null | undefined = AppStorage.get('windowClass')
aboutToAppear(): void {
let isPrivacyMode: boolean = true
if (this.windowClass) {
this.windowClass.setWindowPrivacyMode(isPrivacyMode); // 设置防截屏录屏
}
}
1
浏览88 编辑于2025-03-13 07:12上海
全部评论
最多点赞
最新发布
最早发布
写回答
- 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
- 如您发布的内容为转载内容,请注明内容来源。
发表
我要发帖子
了解社区公约,与您携手共创和谐专业的开发者社区。