app 内集成场景

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

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

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

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: `关闭防窥保护`,
      });
    }
  });
监听方式实现(推荐)
// 监听的回调函数
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}`)
}
}
主动监听触发
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: `有人在窥屏`,
      });
    }
  });
实战案例
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%')
  }
}
Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐