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