鸿蒙HarmonyOS NEXT之无感监听

鸿蒙中存在一些无感监听,这些监听经过系统API封装使用很简单,但是对实际业务开发中有很重要,例如埋点业务、数据统计、行为上报、切面拦截等。

在鸿蒙中Navigation被用来作为路由栈进行页面跳转,如果你想知道页面的路由栈,以及前后切换的页面,可以通过官方提供的无感监听的页面切换navDestinationSwitch。

plain 复制代码
  private _navDestinationListener() {
    this.uiContext?.getUIObserver()
      .on('navDestinationSwitch', (info: uiObserver.NavDestinationSwitchInfo) => {
        let to = info.to;
        if (to != 'navBar') {
          let navName = `${to.name.toString()}#${to.navDestinationId}`;
        }
      });
  }
plain 复制代码
    this.uiContext?.getUIObserver().off('navDestinationSwitch');

点击事件的监听

通过对系统控件设置customProperty,可以通过didClick进行点击事件拦截,相当于对全部的系统控件可以hook处理。

plain 复制代码
  @Builder
  TabBuilder(index: number, controller: TabsController) {
    Column() {
      this.CanvasComponent(index, this.getCanvasRenderingContext())
    }
    .onClick(() => {
    }).layoutWeight(1)
    .customProperty('customPropertyKey', 'value')
  }
plain 复制代码
 private _didClickListener() {
    this.uiContext?.getUIObserver().on('didClick', (event: GestureEvent, frameNode?: FrameNode) => {
      if (frameNode == null) {
        return;
      }
       frameNode.getCustomProperty('customPropertyKey')
    });
  }
plain 复制代码
    this.uiContext?.getUIObserver().off('didClick');

监听TabContent页面的切换事件

在鸿蒙中页签Tabs控件的使用频率比较高,如果想知道哪个子TabContent被点击了,就可以使用监听TabContent页面的切换事件。

plain 复制代码
 private _tabContentUpdateListener() {
    this.uiContext?.getUIObserver().on('tabContentUpdate', (info: uiObserver.TabContentInfo) => {
      if (info.state != uiObserver.TabContentState.ON_SHOW) {
        return;
      }
      this._handleContentUpdateEvent(info.id, info.uniqueId, info.index, info.tabContentUniqueId, true);
      let frameNode = this.uiContext?.getFrameNodeByUniqueId(info.tabContentUniqueId);
    });
  }
plain 复制代码
    this.uiContext?.getUIObserver().off('tabContentUpdate');

![](https://i-blog.csdnimg.cn/img_convert/19cb50e8b0bf17ea2a72d203739ce9cc.png)

Aspect插桩能力

Aspect类用于封装提供切面能力(Aspect Oriented Programming,简写AOP)的接口,这些接口可以用来对类方法进行前后插桩或者替换实现。

在指定的类对象的原方法执行前插入一个函数。addBefore接口执行完成后,都会先执行插入的函数逻辑,再执行指定类对象的原方法。

在指定的类方法执行后插入一段逻辑。最终返回值是插入函数执行后的返回值。

通过插桩处理,在弹框前后做一些逻辑,例如在弹框前后设置变量,就可以判断弹框是否有触发。

plain 复制代码
 util.Aspect.addBefore(CustomDialogController, 'open', false, () => {
      AppStorage.setOrCreate(BusinessUseConstant.CUSTOMDIALOG_ISOPEN, true)
    });
    util.Aspect.addBefore(CustomDialogController, 'close', false, () => {
      AppStorage.setOrCreate(BusinessUseConstant.CUSTOMDIALOG_ISOPEN, false)
    });
相关推荐
Georgewu1 小时前
【HarmonyOS 6】 The target can not be empty. check the build.profile,json5 file of
harmonyos
Georgewu1 小时前
【HarmonyOS 6】Install Failed: error: failed to install bundle.code:9568322
harmonyos
爱笑的眼睛113 小时前
HarmonyOS 应用开发新范式:深入剖析 Stage 模型与 ArkTS 状态管理
华为·harmonyos
爱笑的眼睛114 小时前
深入浅出 HarmonyOS ArkUI 3.0:基于声明式开发范式与高级状态管理构建高性能应用
华为·harmonyos
电手6 小时前
时隔4年麒麟重新登场!华为这8.8英寸新「手机」给我看麻了
华为·智能手机
程序员潘Sir7 小时前
鸿蒙应用开发从入门到实战(一):鸿蒙应用开发概述
harmonyos
敲代码的鱼哇11 小时前
跳转原生系统设置插件 支持安卓/iOS/鸿蒙UTS组件
android·ios·harmonyos
在下历飞雨11 小时前
Kuikly基础之状态管理与数据绑定:让“孤寡”计数器动起来
ios·harmonyos
在下历飞雨11 小时前
Kuikly基础之Kuikly DSL基础组件实战:构建青蛙主界面
ios·harmonyos
HarmonyOS小助手13 小时前
HEIF:更高质量、更小体积,开启 HarmonyOS 图像新体验
harmonyos·鸿蒙·鸿蒙生态