GeorgePandaUI
App开发
HarmonyOS 6
HarmonyOS 5
鸿蒙应用如何屏蔽截屏和录屏,源码和步骤讲解
鸿蒙应用如何屏蔽截屏和录屏,源码和步骤讲解。
金融类或者高密性质的应用APP,对于截屏和录屏场景,某些业务下是禁止不允许。
目前这种场景的需求也是非常有必要的,很多电诈都是通过远程录屏软件,获取到账户密码或者验证码。如此掌控受害人的帐号所有权。
那禁止截屏和录屏在鸿蒙中如何实现呢?


一、结论
鸿蒙系统中通过设置窗口为隐私窗口。来实现禁止用户的录屏和截图。
window窗口对象的setWindowPrivacyMode(boolean)接口。
启用隐私模式(true):禁止对该窗口内容截屏或录屏。
关闭隐私模式(false):恢复窗口正常截屏/录屏能力。

隐私窗口还是很强大的效果,多设备协同,IDE log截图,都无法截图。更别说手机直接截图了,会在底部弹气泡,提示:“当前界面设计隐私内容,不允许截图”
二、代码实现和详细解释
1、首先配置权限,未声明权限将导致隐私模式失效:
{
"module": {
"requestPermissions": [
{
"name": "ohos.permission.PRIVACY_WINDOW"
}
]
}
}
2、设置窗口的隐私模式:
此时就会有个问题,获取窗口。不推荐使用getLastWindow的方式获取窗口,首先该接口是异步,其次该方式是获取最上面的窗口,有很多老应用的弹窗或者全局loading是子窗口实现,会导致getLastWindow获取的是这些东西,不是当前的界面窗口。
当然有的项目组是新项目,或者考虑内存占用问题,也会采用getLastWindow。我这里只是建议,下面也以为缓存窗口来举例。
推荐缓存的窗口的方式,当需要设置目标窗口为隐私模式时直接调用即可:
import { window } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';
import { WindowMgr } from '../mgr/WIndowMgr';
export default class EntryAbility extends UIAbility {
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
}
onWindowStageCreate(windowStage: window.WindowStage): void {
WindowMgr.Ins().setMainWindow(windowStage.getMainWindowSync());
globalThis.mWindowStage = windowStage;
windowStage.loadContent('pages/PrivacyTestPage', (err) => {
if (err.code) {
return;
}
console.log("debugGeorgeGcs", "onWindowStageCreate temp: " + temp);
});
}
}封装的窗口工具单例类:
import { window } from "@kit.ArkUI";
export class WindowMgr {
private TAG: string = 'WindowMgr';
// 单例对象
private static mIns: WindowMgr | null = null;
// 创建单例
public static Ins(): WindowMgr{
if(!WindowMgr.mIns){
WindowMgr.mIns = new WindowMgr();
}
return WindowMgr.mIns;
}
private mainWin: window.Window | null = null;
public setMainWindow(window: window.Window){
this.mainWin = window;
}
/**
* 设置主窗口的隐私模式状态
* @param isPrivacy true为隐私模式
*/
public setMainWindowPrivacyMode(isPrivacy: boolean){
this.mainWin?.setWindowPrivacyMode(isPrivacy).catch((e: Error) => {
console.error(this.TAG, "setWindowPrivacyMode error: " + JSON.stringify(e));
});
}
}截图调用举例:
import { WindowMgr } from '../mgr/WIndowMgr';
/**
* 隐私窗口验证测试界面
*/
@Entry
@Component
struct PrivacyTestPage {
/**
* 推荐在页面生命周期中动态开启/关闭隐私模式:避免频繁调用接口导致异常。
*/
onPageShow(): void {
WindowMgr.Ins().setMainWindowPrivacyMode(true);
}
onPageHide(): void {
WindowMgr.Ins().setMainWindowPrivacyMode(false);
}
build() {
RelativeContainer() {
Text("隐私窗口验证界面")
.id('PrivacyTestPageHelloWorld')
.fontSize($r('app.float.page_text_font_size'))
.fontWeight(FontWeight.Bold)
.alignRules({
center: { anchor: '__container__', align: VerticalAlign.Center },
middle: { anchor: '__container__', align: HorizontalAlign.Center }
})
}
.height('100%')
.width('100%')
}
}
BenYu学习了,加油2025-12-19 21:20 来自北京
- 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
- 如您发布的内容为转载内容,请注明内容来源。
我要提问题
HarmonyOS 设置防截屏失效(API12+)
HarmonyOS 可以区分截屏、投屏、录屏的行为么 (API12+)
HarmonyOS 截屏获取图片 (API12+)
HarmonyOS electron实现录屏
HarmonyOS Web组件中加载的某个h5页面需要做防截屏功能,退出这个页面时需要关闭防截屏,要如何实现 (API12+)
HarmonyOS 如何在App中进行截屏/录屏监听 (API12+)
Flutter如何实现防截屏功能
HarmonyOS 如何在App中监听系统处于录屏/截屏状态 (API12)




































苏公安网备 32011402010933号