Unsubscribing from Common Events in C

When to Use

After completing service requirements, subscribers need to unsubscribe from common events.

Available APIs

For details about the APIs, see oh_commonevent.h.

API Description
CommonEvent_ErrCode OH_CommonEvent_UnSubscribe(const CommonEvent_Subscriber* subscriber) Unsubscribes from a common event.

How to Develop

  1. Include header files.

    #include "hilog/log.h"
    #include "BasicServicesKit/oh_commonevent.h"
    
  2. Add dynamic link libraries to the CMake script.

    target_link_libraries(entry PUBLIC
        libace_napi.z.so
        libhilog_ndk.z.so
        libohcommonevent.so
    )
    
  3. Unsubscribe from a common event.

    After a subscriber subscribes to a common event and meets service requirements, the subscriber can use OH_CommonEvent_UnSubscribe to unsubscribe from the event.

    void Unsubscribe(CommonEvent_Subscriber *subscriber)
    {
        // Unsubscribe from a common event by passing a 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);
    }