harmony开发中如何对当前页面禁止截图

官方给的demo里面主要是针对window窗口进行的一个禁止截屏,相当于整个应用都无法去截屏了。关键属性 setWindowPrivacyMode()第一参数是个布尔值,false代表关闭禁止截屏,ture,代表打开,第二个参数是个回调 

第二个:开启权限 在module.json5里面去开启 

"requestPermissions":{
  "name": "ohos.permission.PRIVACY_WINDOW"
},

整体的思路主要是获取Windows的实例,然后在进入和离开页面的生命周期去调用里面的方法,进行一个实现

思路一:页面设置禁止截屏的开启和关闭,进入页面在 onpageshow生命周期里面调取开启禁止截屏方法,为什么不是aboutToAppear? 当你返回上个页面的时候是不会去走aboutToAppear这个生命周期的
页面离开用onPageHide调用关闭禁止截屏方法,  

  private context: common.BaseContext = getContext(this) as common.BaseContext
screenshots(bol: boolean) {

    window.getLastWindow(this.context)
      .then((windowClass: window.Window) => {
        try {
          windowClass.setWindowPrivacyMode(bol, (err: BusinessError) => {
            const errCode: number = err.code;
            if (errCode) {
              console.error(`Failed to set the window to privacy mode. Cause code: ${err.code}, message: ${err.message}`);
              return;
            }
            console.info('Succeeded in setting the window to privacy mode.');
          });
        } catch (exception) {
          console.error(`Failed to set the window to privacy mode. Cause code: ${exception.code}, message: ${exception.message}`);
        }
      })
      .catch(() => {
      })
  }

思路二:在EntryAbility 里面获取windowclass ,然后用

AppStorage.setOrCreate('windowStage',windowClass);全局变量,后面就可以在需要的页面去使用, 
@StorageLink('windowStage') windowClass?:window.Window=AppStorage.get('windowStage')

需要禁止截屏的页面里面去使用,同样也是在上述的两个生命周期里面去使用

 (this.windowClass as window.Window).setWindowPrivacyMode(true,(err: BusinessError)=>{   const errCode: number = err.code;
  if (errCode) {
     console.error('Failed to set the window to be touchable. Cause:' + JSON.stringify(err));
     return;
   }

 })

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值