openharmony 鸿蒙 native-common-event-unsubscription

2025-06-12 浏览 (1)

取消订阅公共事件(C/C++)

场景介绍

订阅者在完成业务需求之后,需要取消订阅公共事件。

接口说明

详细的API说明请参考CommonEvent API参考

接口名描述
CommonEvent_ErrCode OH_CommonEvent_UnSubscribe(const CommonEvent_Subscriber* subscriber)取消订阅公共事件。

开发步骤

  1. 引用头文件。

    #include <cstdint>
    #include "hilog/log.h"
    #include "BasicServicesKit/oh_commonevent.h"
    
  2. 在CMake脚本中添加动态链接库。

    target_link_libraries(entry PUBLIC
        libace_napi.z.so
        libhilog_ndk.z.so
        libohcommonevent.so
    )
    
  3. 取消订阅公共事件。

    订阅者订阅公共事件并完成业务需求后,可以通过OH_CommonEvent_UnSubscribe主动取消订阅事件。

    void Unsubscribe(CommonEvent_Subscriber* subscriber) {
        // 通过传入订阅者来退订事件
        int32_t ret = OH_CommonEvent_UnSubscribe(subscriber);
        OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "OH_CommonEvent_UnSubscribe ret <%{public}d>.", ret);
    }
    

你可能感兴趣的鸿蒙文章

harmony 鸿蒙进程线程通信

harmony 鸿蒙公共事件简介

harmony 鸿蒙公共事件发布

harmony 鸿蒙移除粘性公共事件(仅对系统应用开放)

harmony 鸿蒙静态订阅公共事件(仅对系统应用开放)

harmony 鸿蒙动态订阅公共事件

harmony 鸿蒙取消动态订阅公共事件

harmony 鸿蒙使用Emitter进行线程间通信

harmony 鸿蒙发布公共事件(C/C++)

harmony 鸿蒙订阅公共事件(C/C++)

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/KQ6fhl