• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 发布公共事件(C/C++)
2
3
4## 场景介绍
5
6当需要发布某个自定义公共事件时,可以通过[OH_CommonEvent_Publish](../../reference/apis-basic-services-kit/capi-common-event.md#oh_commonevent_publish)和[OH_CommonEvent_PublishWithInfo](../../reference/apis-basic-services-kit/capi-common-event.md#oh_commonevent_publishwithinfo)方法发布事件。发布的公共事件可以携带数据,供订阅者解析并进行下一步处理。
7
8## 接口说明
9
10详细的API说明请参考[CommonEvent API参考](../../reference/apis-basic-services-kit/capi-common-event.md)。
11
12| 接口名                               | 描述                                                             |
13| ------------------------------------ | ---------------------------------------------------------------- |
14|[struct CommonEvent_PublishInfo](../../reference/apis-basic-services-kit/capi-common-event.md#commonevent_publishinfo)|发布自定义公共事件时使用的公共事件属性对象。|
15|[CommonEvent_ErrCode OH_CommonEvent_Publish(const char* event)](../../reference/apis-basic-services-kit/capi-common-event.md#oh_commonevent_publish)|发布自定义公共事件。|
16|[CommonEvent_ErrCode OH_CommonEvent_PublishWithInfo(const char* event, const CommonEvent_PublishInfo* info)](../../reference/apis-basic-services-kit/capi-common-event.md#oh_commonevent_publishwithinfo)| 发布带有指定属性的自定义公共事件。 |
17|[CommonEvent_PublishInfo* OH_CommonEvent_CreatePublishInfo(bool ordered)](../../reference/apis-basic-services-kit/capi-common-event.md#oh_commonevent_createpublishinfo)|创建公共事件属性对象。|
18|[void OH_CommonEvent_DestroyPublishInfo(CommonEvent_PublishInfo* info)](../../reference/apis-basic-services-kit/capi-common-event.md#oh_commonevent_destroypublishinfo)|销毁公共事件属性对象。|
19|[CommonEvent_Parameters* OH_CommonEvent_CreateParameters()](../../reference/apis-basic-services-kit/capi-common-event.md#oh_commonevent_createparameters)|创建公共事件附加信息对象。|
20|[void OH_CommonEvent_DestroyParameters(CommonEvent_Parameters* param)](../../reference/apis-basic-services-kit/capi-common-event.md#oh_commonevent_destroyparameters)|销毁公共事件附加信息对象。|
21
22## 开发步骤
23
241. 引用头文件。
25
26   ```c++
27   #include <cstdint>
28   #include <cstdio>
29   #include <cwchar>
30   #include <string.h>
31   #include "hilog/log.h"
32   #include "BasicServicesKit/oh_commonevent.h"
33   ```
34
352. 在CMake脚本中添加动态链接库。
36
37   ```txt
38   target_link_libraries(entry PUBLIC
39       libace_napi.z.so
40       libhilog_ndk.z.so
41       libohcommonevent.so
42   )
43   ```
44
453. (可选)创建公共事件属性对象。
46
47   发布携带数据的自定义公共事件时,需要通过[OH_CommonEvent_CreatePublishInfo](../../reference/apis-basic-services-kit/capi-common-event.md#oh_commonevent_createpublishinfo)创建公共事件属性对象,并通过以下接口设置公共事件属性。
48
49   ```c++
50   // 创建并添加公共事件属性附加信息
51   CommonEvent_Parameters* CreateParameters()
52   {
53       int32_t ret = -1;
54       // 创建公共事件附加信息
55       CommonEvent_Parameters* param = OH_CommonEvent_CreateParameters();
56
57       // 设置int类型附加信息和key
58       ret = OH_CommonEvent_SetIntToParameters(param, "intKey", 10);
59       OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "OH_CommonEvent_SetIntToParameters ret <%{public}d>.", ret);
60
61       // 设置int数组类型附加信息和key
62       int intArray[] = {123, 234, 567};
63       size_t arraySize = 3;
64       ret = OH_CommonEvent_SetIntArrayToParameters(param, "intArrayKey", intArray, arraySize);
65       OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "OH_CommonEvent_SetIntArrayToParameters ret <%{public}d>.", ret);
66
67       // 设置long类型附加信息和key
68       ret = OH_CommonEvent_SetLongToParameters(param, "longKey", 2147483646);
69       OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "OH_CommonEvent_SetLongToParameters ret <%{public}d>.", ret);
70
71       // 设置long数组类型附加信息和key
72       long longArray[] = {2147483646, 555, 2147483645};
73       ret = OH_CommonEvent_SetLongArrayToParameters(param, "longArrayKey", longArray, arraySize);
74       OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "OH_CommonEvent_SetLongArrayToParameters ret <%{public}d>.", ret);
75
76       // 设置double类型附加信息和key
77       ret = OH_CommonEvent_SetDoubleToParameters(param, "doubleKey", 11.22);
78       OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "OH_CommonEvent_SetDoubleToParameters ret <%{public}d>.", ret);
79
80       // 设置double数组类型附加信息和key
81       double doubleArray[] = {11.22, 33.44, 55.66};
82       ret = OH_CommonEvent_SetDoubleArrayToParameters(param, "doubleArrayKey", doubleArray, arraySize);
83       OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "OH_CommonEvent_SetDoubleArrayToParameters ret <%{public}d>.", ret);
84
85       // 设置boolean类型附加信息和key
86       ret = OH_CommonEvent_SetBoolToParameters(param, "boolKey", true);
87       OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "OH_CommonEvent_SetBoolToParameters ret <%{public}d>.", ret);
88
89       // 设置boolean数组类型附加信息和key
90       bool boolArray[] = {true, false, true};
91       ret = OH_CommonEvent_SetBoolArrayToParameters(param, "boolArrayKey", boolArray, arraySize);
92       OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "OH_CommonEvent_SetBoolArrayToParameters ret <%{public}d>.", ret);
93
94       // 设置char类型附加信息和key
95       ret = OH_CommonEvent_SetCharToParameters(param, "charKey", 'A');
96       OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "OH_CommonEvent_SetCharToParameters ret <%{public}d>.", ret);
97
98       // 设置char数组类型附加信息和key
99       char* value= "AAAAAAAAAAAAAA";
100       size_t valueLength = strlen(value);
101       ret = OH_CommonEvent_SetCharArrayToParameters(param, "charArrayKey", value, valueLength);
102       OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "OH_CommonEvent_SetCharArrayToParameters ret <%{public}d>.", ret);
103       return param;
104   }
105
106   // 设置公共事件属性
107   void SetPublishInfo(const char* bundleName, const char* permissions[], int32_t num, const int32_t code, const char* data)
108   {
109       int32_t ret = -1;
110       // 创建publishInfo,设置是否为有序公共事件,取值为true,表示有序公共事件;取值为false,表示无序公共事件
111       CommonEvent_PublishInfo* info = OH_CommonEvent_CreatePublishInfo(true);
112
113       // 设置公共事件包名称
114       ret = OH_CommonEvent_SetPublishInfoBundleName(info, bundleName);
115       OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "OH_CommonEvent_SetPublishInfoBundleName ret <%{public}d>.", ret);
116
117       // 设置公共事件权限,参数为权限数组和权限的数量
118       ret = OH_CommonEvent_SetPublishInfoPermissions(info, permissions, num);
119       OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "OH_CommonEvent_SetPublishInfoPermissions ret <%{public}d>.", ret);
120
121       // 设置公共事件结果码
122       ret = OH_CommonEvent_SetPublishInfoCode(info, code);
123       OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "OH_CommonEvent_SetPublishInfoCode ret <%{public}d>.", ret);
124
125       // 设置公共事件结果数据
126       size_t dataLength = strlen(data);
127       ret = OH_CommonEvent_SetPublishInfoData(info, data, dataLength);
128       OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "OH_CommonEvent_SetPublishInfoData ret <%{public}d>.", ret);
129
130       // 设置公共事件附加信息
131       CommonEvent_Parameters* param = CreateParameters();
132       ret = OH_CommonEvent_SetPublishInfoParameters(info, param);
133       OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "OH_CommonEvent_SetPublishInfoParameters ret <%{public}d>.", ret);
134   }
135   ```
136
1374. 发布公共事件。
138
139   - 通过[OH_CommonEvent_Publish](../../reference/apis-basic-services-kit/capi-common-event.md#oh_commonevent_publish)发布不携带信息的公共事件。
140
141     > **说明:**
142     >
143     > 不携带信息的公共事件,只能发布为无序公共事件。
144
145     ```c++
146     void Publish(const char* event)
147     {
148         int32_t ret = OH_CommonEvent_Publish(event);
149         OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "OH_CommonEvent_Publish ret <%{public}d>.", ret);
150     }
151     ```
152
153   - 通过[OH_CommonEvent_PublishWithInfo](../../reference/apis-basic-services-kit/capi-common-event.md#oh_commonevent_publishwithinfo)发布携带信息的公共事件。
154
155     ```c++
156     void PublishWithInfo(const char* event, CommonEvent_PublishInfo* info)
157     {
158         // 创建时带入公共事件属性对象
159         int32_t ret = OH_CommonEvent_PublishWithInfo(event, info);
160         OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "OH_CommonEvent_PublishWithInfo ret <%{public}d>.", ret);
161     }
162     ```
163
1645. 销毁公共事件对象。
165
166   如果后续无需使用已创建的公共事件对象来发布公共事件,需要先通过[OH_CommonEvent_DestroyParameters](../../reference/apis-basic-services-kit/capi-common-event.md#oh_commonevent_destroyparameters)销毁`CommonEvent_Parameters`对象,然后再通过[OH_CommonEvent_DestroyPublishInfo](../../reference/apis-basic-services-kit/capi-common-event.md#oh_commonevent_destroypublishinfo)销毁公共事件对象。
167
168   ```c++
169   void DestroyPublishInfo(CommonEvent_Parameters* param, CommonEvent_PublishInfo* info)
170   {
171       // 先销毁Parameters
172       OH_CommonEvent_DestroyParameters(param);
173       param = nullptr;
174       // 销毁PublishInfo
175       OH_CommonEvent_DestroyPublishInfo(info);
176       info = nullptr;
177   }
178   ```