HarmonyOS6 新特性之防窥保护DlpAntiPeep
在公共场合(比如地铁、电梯里),用户在使用手机查看敏感信息(如银行余额、私密聊天)时,经常会担心被旁边的人偷看。HarmonyOS6 提供了系统级的防窥保护能力系统会通过前置摄像头和人脸解锁记录,智能判断当前看屏幕的人是不是机主,或者有没有其他人在背后偷窥。如果检测到非机主或多人在看屏幕,系统会将状态判定为“窥屏”。我们的 App 只要接入了这个功能,就可以在检测到别人偷看时,自动给页面加上蒙层,
·
1. 什么是防窥保护?
在公共场合(比如地铁、电梯里),用户在使用手机查看敏感信息(如银行余额、私密聊天)时,经常会担心被旁边的人偷看。
HarmonyOS6 提供了系统级的防窥保护能力:
- 系统会通过前置摄像头和人脸解锁记录,智能判断当前看屏幕的人是不是机主,或者有没有其他人在背后偷窥。
- 如果检测到非机主或多人在看屏幕,系统会将状态判定为“窥屏”。
- 我们的 App 只要接入了这个功能,就可以在检测到别人偷看时,自动给页面加上蒙层,或者隐藏掉敏感的支付记录、浏览记录等。
2. 第一步:申请必要权限
要使用系统的人脸防窥状态,App 首先需要向系统申请权限。
打开你项目中的 entry/src/main/module.json5 文件,在 requestPermissions 数组中添加以下权限:
"requestPermissions": [
{
"name": "ohos.permission.DLP_GET_HIDE_STATUS",
"reason": "$string:dlp_anti_peep_reason",
"usedScene": {
"abilities": [
"EntryAbility"
],
"when": "inuse"
}
}
]
💡 小贴士: 用户还需要在手机的 “设置 > 隐私和安全 > 防窥保护” 中为你的 App 开启开关,防窥功能才会真正生效。
3. 第二步:认识核心 API
在使用代码前,我们需要导入 DeviceSecurityKit 中的 dlpAntiPeep 模块:
import { dlpAntiPeep } from '@kit.DeviceSecurityKit';
它主要提供了 3 个核心能力:
- 判断开关是否开启:
isDlpAntiPeepSwitchOn()
返回一个 Promise,告诉我们用户有没有在系统设置里允许我们使用防窥。 - 主动获取当前状态:
getDlpAntiPeepInfo()
直接返回当前的窥屏状态枚举值(PASS代表安全没人看,HIDE代表有人偷窥)。 - 实时监听状态(⭐ 推荐):
on('dlpAntiPeep', callback)
一旦有人把头凑过来,系统立刻通知我们,App 就能瞬间做出反应!
4. 第三步:实战代码演练
下面是一个完整的页面示例,当没人看时显示正常内容,当有人偷看时,立刻显示警告文字!
import { dlpAntiPeep } from '@kit.DeviceSecurityKit';
import { promptAction } from '@kit.ArkUI';
@Entry
@Component
struct AntiPeepDemo {
// 定义一个状态变量,记录当前是否处于被窥屏状态
@State isPeeping: boolean = false;
// 1. 定义监听回调函数
dlpCallback = (status: dlpAntiPeep.DlpAntiPeepStatus) => {
if (status === dlpAntiPeep.DlpAntiPeepStatus.HIDE) {
// 😱 发现有人偷窥!
this.isPeeping = true;
} else if (status === dlpAntiPeep.DlpAntiPeepStatus.PASS) {
// 😌 安全,只有机主一个人在看
this.isPeeping = false;
}
}
// 2. 在页面加载时开启监听
aboutToAppear() {
try {
// 先检查用户是否开启了开关
dlpAntiPeep.isDlpAntiPeepSwitchOn().then((isOpen: boolean) => {
if (isOpen) {
// 如果开启了,就注册防窥状态监听
dlpAntiPeep.on('dlpAntiPeep', this.dlpCallback);
} else {
promptAction.openToast({ message: '请在系统设置中开启防窥保护开关' });
}
});
} catch (err) {
console.error('防窥保护初始化失败:', JSON.stringify(err));
}
}
// 3. 在页面销毁时取消监听(养成好习惯,防止内存泄漏)
aboutToDisappear() {
try {
dlpAntiPeep.off('dlpAntiPeep', this.dlpCallback);
} catch (err) {
console.error('取消防窥监听失败:', JSON.stringify(err));
}
}
build() {
Column() {
if (this.isPeeping) {
// 🚨 被偷窥时的 UI 展示
Column() {
Text('⚠️ 警告')
.fontSize(50)
.fontColor(Color.Red)
.fontWeight(FontWeight.Bold)
Text('检测到非机主或多人在看屏幕,内容已隐藏!')
.fontSize(20)
.fontColor(Color.White)
.margin({ top: 20 })
}
.width('100%')
.height('100%')
.backgroundColor(Color.Black)
.justifyContent(FlexAlign.Center)
} else {
// ✅ 正常情况下的 UI 展示
Column() {
Text('💰 我的小金库')
.fontSize(30)
.fontWeight(FontWeight.Bold)
.margin({ top: 50 })
Text('余额:¥ 99,999,999.00')
.fontSize(24)
.fontColor('#e6a23c')
.margin({ top: 20 })
Text('(只有机主才能看到这行字哦~)')
.fontSize(16)
.fontColor(Color.Gray)
.margin({ top: 40 })
}
.width('100%')
.height('100%')
.alignItems(HorizontalAlign.Center)
}
}
.width('100%')
.height('100%')
}
}
5. 总结
通过引入 @kit.DeviceSecurityKit,我们可以快速高效地给 App 加上高级的安全特性。在金融、社交、企业办公类 App 中,防窥保护是提升用户安全感的神兵利器!
更多推荐
所有评论(0)