• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 取消订阅公共事件(C/C++)
2
3
4## 场景介绍
5
6订阅者在完成业务需求之后,需要取消订阅公共事件。
7
8## 接口说明
9
10详细的API说明请参考[CommonEvent API参考](../../reference/apis-basic-services-kit/capi-common-event.md)。
11
12| 接口名                               | 描述                                                             |
13| ------------------------------------ | ---------------------------------------------------------------- |
14|[CommonEvent_ErrCode OH_CommonEvent_UnSubscribe(const CommonEvent_Subscriber* subscriber)](../../reference/apis-basic-services-kit/capi-common-event.md#oh_commonevent_unsubscribe)|创建订阅者信息。|
15
16## 开发步骤
17
181. 引用头文件。
19
20   ```c++
21   #include <cstdint>
22   #include "hilog/log.h"
23   #include "BasicServicesKit/oh_commonevent.h"
24   ```
25
262. 在CMake脚本中添加动态链接库。
27
28   ```txt
29   target_link_libraries(entry PUBLIC
30       libace_napi.z.so
31       libhilog_ndk.z.so
32       libohcommonevent.so
33   )
34   ```
35
36
373. 取消订阅公共事件。
38
39   订阅者订阅公共事件并完成业务需求后,可以通过[OH_CommonEvent_UnSubscribe](../../reference/apis-basic-services-kit/capi-common-event.md#oh_commonevent_unsubscribe)主动取消订阅事件。
40
41   ```c++
42   void Unsubscribe(CommonEvent_Subscriber* subscriber) {
43       // 通过传入订阅者来退订事件
44       int32_t ret = OH_CommonEvent_UnSubscribe(subscriber);
45       OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "OH_CommonEvent_UnSubscribe ret <%{public}d>.", ret);
46   }
47   ```