鸿蒙6应用内集成防窥保护

app 内集成场景

支持应用根据屏幕窥视状态保护机主隐私,如拉起系统级蒙层遮盖窗口,非机主状态下不进行个性化推荐,隐藏浏览记录、支付记录、收藏记录等敏感信息。其中系统使用智能判断将长期通过人脸解锁手机的人作为防窥保护的机主

首先在 module.json5 申请权限
json 复制代码
 "requestPermissions": [
      {
        "name": "ohos.permission.DLP_GET_HIDE_STATUS"
      }
    ],
在设置/隐私和安全/防窥保护/打开 app 的权限

dlpAntiPeep.isDlpAntiPeepSwitchOn();接口可以返回当前是否开启了 app 权限 返回值为Promise<boolean>

typescript 复制代码
Button("click")
  .fontSize(100)
  .onClick(async (event: ClickEvent) => {
    const isOpen = await dlpAntiPeep.isDlpAntiPeepSwitchOn();
    if (isOpen) {
      this.getUIContext().getPromptAction().showToast({
        message: `开启防窥保护`,
      });
    } else {
      this.getUIContext().getPromptAction().showToast({
        message: `关闭防窥保护`,
      });
    }
  });
监听方式实现(推荐)
typescript 复制代码
// 监听的回调函数
dlpCallback(dlpAntPeepStatus:dlpAntiPeep.DlpAntiPeepStatus){
    if(dlpAntPeepStatus == dlpAntiPeep.DlpAntiPeepStatus.HIDE){
      // 表示有人窥屏 可以在里面实现相应的业务逻辑
    }
    if(dlpAntPeepStatus == dlpAntiPeep.DlpAntiPeepStatus.PASS){
      //表示没人窥屏
    }
  }

// 在aboutToAppear生命周期中设置监听
aboutToAppear(): void {
try {
    dlpAntiPeep.on('dlpAntiPeep',this.dlpCallback)
}catch (e){
    hilog.error(0x0001,'[dlpAntiPeep err]',`message:${e.message}`)
}
}
主动监听触发
typescript 复制代码
Button("click")
  .fontSize(100)
  .onClick((event: ClickEvent) => {
    // 该接口用于获取当前窥屏状态
    const res = dlpAntiPeep.getDlpAntiPeepInfo();
    // 返回值可以用枚举值来表示
    if (res == dlpAntiPeep.DlpAntiPeepStatus.PASS) {
      this.getUIContext().getPromptAction().showToast({
        message: `没人窥屏`,
      });
    }
    if (res == dlpAntiPeep.DlpAntiPeepStatus.HIDE) {
      this.getUIContext().getPromptAction().showToast({
        message: `有人在窥屏`,
      });
    }
  });
实战案例
typescript 复制代码
import { dlpAntiPeep } from '@kit.DeviceSecurityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BulletChat } from '@shuishenhuole/bulletchat';

@Entry
@ComponentV2
struct Index {
  @Local isDip:boolean = false

  callback(dlpAntPeepStatus:dlpAntiPeep.DlpAntiPeepStatus){
    if(dlpAntPeepStatus == dlpAntiPeep.DlpAntiPeepStatus.HIDE){
      // 可以使用当前的状态变量也可以使用持久化的变量 根据业务代码实现逻辑
      this.isDip = true
    }
    if(dlpAntPeepStatus == dlpAntiPeep.DlpAntiPeepStatus.PASS){
    }
  }

  aboutToAppear(): void {
    try {
      // bind防止this丢失 也可以直接使用箭头函数
      dlpAntiPeep.on('dlpAntiPeep',this.callback.bind(this))

    // 箭头函数写法
    //       dlpAntiPeep.on('dlpAntiPeep',(dlpAntPeepStatus:dlpAntiPeep.DlpAntiPeepStatus)=>{
    //     if(dlpAntPeepStatus == dlpAntiPeep.DlpAntiPeepStatus.HIDE){
    //       // 可以使用当前的状态变量也可以使用持久化的变量 根据业务代码实现逻辑
    //       this.isDip = true
    //     }
    //     if(dlpAntPeepStatus == dlpAntiPeep.DlpAntiPeepStatus.PASS){
    //     }
    //   })


    }
    catch (err) {
      hilog.error(0x0002,'dlpAntiPeep on error',`message:${err.message}`)
    }
  }

  build() {
    Flex({
      justifyContent:FlexAlign.Center,
      alignItems:ItemAlign.Center
    }) {
      if(this.isDip){
        // BulletChat为我的开源组件(需要>=2.0.2版本)
        // ohpm install @shuishenhuole/bulletchat
        BulletChat({
          text:"看看看就知道窥屏",
          desc:"desc",
          title:"title",
          OpenLink:"openlink",
          AutoLandScape:false,
          fontOption:{
            fontSize:75,
            textShadow:{
              radius:20,
              color:Color.Pink
            },
            marqueeOptions:{
              start:true,
              step:10
            }
          }
        })
          .onClick(()=>{
            this.isDip = false
          })
      }else{
        Column(){
          Text("假设这是一个见不得人的app")
            .fontWeight(FontWeight.Bold)
          Text("power by shuishenhuole")
            .fontWeight(FontWeight.Bolder)
        }
      }
    }
    .height('100%')
    .width('100%')
  }
}
相关推荐
道路与代码之旅2 小时前
Windows 10 中以 WSL 驱 Ubuntu 记
linux·windows·ubuntu
张人玉2 小时前
大数据Hadoop系列——在ubuntu上安装Hive 嵌入式
大数据·hadoop·ubuntu
500842 小时前
鸿蒙 Flutter 接入鸿蒙系统能力:通知(本地 / 推送)与后台任务
java·flutter·华为·性能优化·架构
盐焗西兰花3 小时前
鸿蒙学习实战之路 - 应用间链接最佳实践
学习·华为·harmonyos
大雷神3 小时前
[鸿蒙2025领航者闯关]HarmonyOS中开发高德地图第三篇:地图控制与UI设置
harmonyos
你们补药再卷啦3 小时前
电脑centos7替换为Ubuntu 22.04
ubuntu
人工智能训练3 小时前
openEuler系统中home文件夹下huawei、HwHiAiUser、lost+found 文件夹的区别和作用
linux·运维·服务器·人工智能·windows·华为·openeuler
米花町的小侦探3 小时前
Ubuntu安装多版本golang
linux·ubuntu·golang
●VON13 小时前
从单机应用到分布式调度:基于 HarmonyOS 构建车-空协同任务引擎
学习·华为·harmonyos·openharmony·开源鸿蒙