鸿蒙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%')
  }
}
相关推荐
猛扇赵四那边好嘴.32 分钟前
Flutter 框架跨平台鸿蒙开发 - 睡眠记录应用开发教程
flutter·华为·harmonyos
鸣弦artha34 分钟前
Flutter框架跨平台鸿蒙开发——InheritedWidget基础使用-计数器案例
android·flutter·harmonyos
s_daqing34 分钟前
ubuntu(arm)使用nginx安装静态服务器
服务器·nginx·ubuntu
皓月盈江2 小时前
个人计算机Linux Debian桌面操作系统上网安全防护措施
linux·ubuntu·网络安全·debian·桌面操作系统·上网安全防护措施
弓.长.2 小时前
React Native 鸿蒙跨平台开发:长按菜单效果
react native·react.js·harmonyos
No Silver Bullet2 小时前
HarmonyOS NEXT开发进阶(二十):纯血鸿蒙应用(HarmonyOS NEXT)在尝试解析域名时失败问题修复
华为·harmonyos
小白阿龙2 小时前
鸿蒙+flutter 跨平台开发——决策工具的开发实现
flutter·华为·harmonyos·鸿蒙
欣然~2 小时前
鸿蒙系统专利管理系统
华为·harmonyos
南村群童欺我老无力.2 小时前
Flutter 框架跨平台鸿蒙开发 - 打造经典连连看游戏
flutter·游戏·华为·harmonyos
弓.长.3 小时前
React Native 鸿蒙跨平台开发:实现一个模拟计算器
react native·react.js·harmonyos