鸿蒙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 小时前
HexStrike-AI人工智能 渗透测试学习(Metasploitable2 192.168.1.4)完整流程总结
学习·ubuntu
时光803.2 小时前
快速搭建青龙面板Docker教程
windows·ubuntu·bash·httpx
不爱吃糖的程序媛3 小时前
ArkUI-X 6.0.0 Release发布
华为·harmonyos
w139548564225 小时前
Flutter跨平台组件集成框架鸿蒙化使用指南
flutter·华为·harmonyos
进击的前栈6 小时前
Flutter跨平台开发鸿蒙化HTTP解析工具包使用指南
flutter·http·harmonyos
进击的前栈7 小时前
Flutter跨平台开发鸿蒙化HTTP测试工具包使用指南
flutter·http·harmonyos
DARLING Zero two♡8 小时前
拒绝“环境劝退”:Llama-2-7b 在昇腾 NPU 上的工程化部署与深度故障排查实录
华为·llama·gpu算力
你好helloworld11 小时前
ubuntu安装protobuf
linux·运维·ubuntu
花开彼岸天~12 小时前
Flutter跨平台开发鸿蒙化定位服务组件使用指南
flutter·开源·harmonyos
特立独行的猫a13 小时前
移植开源软件Notepad--(NDD)到鸿蒙PC:环境搭建与配置
notepad++·开源软件·harmonyos·鸿蒙pc·notpad--