• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.commonEvent (公共事件模块)
2
3本模块提供了公共事件的能力,包括公共事件的权限列表,发布公共事件,订阅或取消订阅公共事件,获取或修改公共事件结果代码、结果数据等。
4
5> **说明:**
6> - 从API Version 9开始,该接口不再维护,推荐使用新接口[@ohos.commonEventManager](js-apis-commonEventManager.md)。
7>
8> - 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
9
10## 导入模块
11
12```ts
13import CommonEvent from '@ohos.commonEvent';
14```
15
16## Support
17
18系统公共事件是指由系统服务或系统应用发布的事件,订阅这些系统公共事件需要特定的权限。发布或订阅这些事件需要使用如下链接中的枚举定义。
19
20全部系统公共事件枚举定义请参见[系统公共事件定义](./commonEvent-definitions.md)。
21
22## CommonEvent.publish<sup>(deprecated)</sup>
23
24publish(event: string, callback: AsyncCallback\<void>): void
25
26发布公共事件(回调形式)。
27
28> **说明:**
29> 从 API version 7开始支持,从API version 9开始废弃。建议使用[commonEventManager.publish](js-apis-commonEventManager.md#commoneventmanagerpublish)替代。
30
31**系统能力:** `SystemCapability.Notification.CommonEvent`
32
33**参数:**
34
35| 参数名     | 类型                 | 必填 | 说明                   |
36| -------- | -------------------- | ---- | ---------------------- |
37| event    | string               | 是   | 表示要发送的公共事件。 |
38| callback | AsyncCallback\<void> | 是   | 表示被指定的回调方法。 |
39
40**示例:**
41
42```ts
43import Base from '@ohos.base';
44
45//发布公共事件回调
46function publishCB(err:Base.BusinessError) {
47	if (err.code) {
48        console.error(`publish failed, code is ${err.code}`);
49    } else {
50        console.info("publish");
51    }
52}
53
54//发布公共事件
55CommonEvent.publish("event", publishCB);
56```
57
58## CommonEvent.publish<sup>(deprecated)</sup>
59
60publish(event: string, options: CommonEventPublishData, callback: AsyncCallback\<void>): void
61
62以回调的形式发布公共事件。
63
64> **说明:**
65> 从 API version 7开始支持,从API version 9开始废弃。建议使用[commonEventManager.publish](js-apis-commonEventManager.md#commoneventmanagerpublish-1)替代。
66
67**系统能力:** `SystemCapability.Notification.CommonEvent`
68
69**参数:**
70
71| 参数名     | 类型                   | 必填 | 说明                   |
72| -------- | ---------------------- | ---- | ---------------------- |
73| event    | string                 | 是   | 表示要发布的公共事件。  |
74| options  | [CommonEventPublishData](./js-apis-inner-commonEvent-commonEventPublishData.md) | 是   | 表示发布公共事件的属性。 |
75| callback | AsyncCallback\<void>   | 是   | 表示被指定的回调方法。  |
76
77**示例:**
78
79
80```ts
81import Base from '@ohos.base';
82import CommonEventManager from '@ohos.commonEventManager';
83
84//公共事件相关信息
85let options:CommonEventManager.CommonEventPublishData = {
86	code: 0,			 //公共事件的初始代码
87	data: "initial data",//公共事件的初始数据
88	isOrdered: true	 //有序公共事件
89}
90
91//发布公共事件回调
92function publishCB(err:Base.BusinessError) {
93	if (err.code) {
94        console.error(`publish failed, code is ${err.code}`);
95    } else {
96        console.info("publish");
97    }
98}
99
100//发布公共事件
101CommonEvent.publish("event", options, publishCB);
102```
103
104## CommonEvent.publishAsUser<sup>(deprecated)</sup>
105
106publishAsUser(event: string, userId: number, callback: AsyncCallback\<void>): void
107
108以回调的形式向指定用户发布公共事件。
109
110> **说明:**
111> 从 API version 8开始支持,从API version 9开始废弃。建议使用[commonEventManager.publishAsUser](js-apis-commonEventManager.md#commoneventmanagerpublishasuser)替代。
112
113**系统能力:** `SystemCapability.Notification.CommonEvent`
114
115**系统API**:此接口为系统接口,三方应用不支持调用。
116
117**参数:**
118
119| 参数名     | 类型                 | 必填 | 说明                               |
120| -------- | -------------------- | ---- | ---------------------------------- |
121| event    | string               | 是   | 表示要发送的公共事件。             |
122| userId   | number               | 是   | 表示指定向该用户ID发送此公共事件。 |
123| callback | AsyncCallback\<void> | 是   | 表示被指定的回调方法。             |
124
125**示例:**
126
127```ts
128import Base from '@ohos.base';
129
130// 发布公共事件回调
131function publishCB(err:Base.BusinessError) {
132	if (err.code) {
133        console.error(`publishAsUser failed, code is ${err.code}`);
134    } else {
135        console.info("publishAsUser");
136    }
137}
138
139// 指定发送的用户
140let userId = 100;
141
142// 发布公共事件
143CommonEvent.publishAsUser("event", userId, publishCB);
144```
145
146## CommonEvent.publishAsUser<sup>(deprecated)</sup>
147
148publishAsUser(event: string, userId: number, options: CommonEventPublishData, callback: AsyncCallback\<void>): void
149
150以回调形式向指定用户发布公共事件并指定发布信息。
151
152> **说明:**
153> 从 API version 8开始支持,从API version 9开始废弃。建议使用[commonEventManager.publishAsUser](js-apis-commonEventManager.md#commoneventmanagerpublishasuser-1)替代。
154
155**系统能力:** `SystemCapability.Notification.CommonEvent`
156
157**系统API**:此接口为系统接口,三方应用不支持调用。
158
159**参数:**
160
161| 参数名     | 类型                   | 必填 | 说明                   |
162| -------- | ---------------------- | ---- | ---------------------- |
163| event    | string                 | 是   | 表示要发布的公共事件。  |
164| userId   | number | 是 | 表示指定向该用户ID发送此公共事件。 |
165| options  | [CommonEventPublishData](./js-apis-inner-commonEvent-commonEventPublishData.md) | 是   | 表示发布公共事件的属性。 |
166| callback | AsyncCallback\<void>   | 是   | 表示被指定的回调方法。  |
167
168**示例:**
169
170
171```ts
172import Base from '@ohos.base';
173import CommonEventManager from '@ohos.commonEventManager';
174
175// 公共事件相关信息
176let options:CommonEventManager.CommonEventPublishData = {
177	code: 0,			 // 公共事件的初始代码
178	data: "initial data",// 公共事件的初始数据
179}
180
181// 发布公共事件回调
182function publishCB(err:Base.BusinessError) {
183    if (err.code) {
184        console.error(`publishAsUser failed, code is ${err.code}`);
185    } else {
186        console.info("publishAsUser");
187    }
188}
189
190// 指定发送的用户
191let userId = 100;
192
193// 发布公共事件
194CommonEvent.publishAsUser("event", userId, options, publishCB);
195```
196
197## CommonEvent.createSubscriber<sup>(deprecated)</sup>
198
199createSubscriber(subscribeInfo: CommonEventSubscribeInfo, callback: AsyncCallback\<CommonEventSubscriber>): void
200
201以回调形式创建订阅者。
202
203> **说明:**
204>从 API version 7开始支持,从API version 9开始废弃。建议使用[commonEventManager.createSubscriber](js-apis-commonEventManager.md#commoneventmanagercreatesubscriber)替代。
205
206**系统能力:** `SystemCapability.Notification.CommonEvent`
207
208**参数:**
209
210| 参数名          | 类型                                                         | 必填 | 说明                       |
211| ------------- | ------------------------------------------------------------ | ---- | -------------------------- |
212| subscribeInfo | [CommonEventSubscribeInfo](./js-apis-inner-commonEvent-commonEventSubscribeInfo.md)        | 是   | 表示订阅信息。             |
213| callback      | AsyncCallback\<[CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md)> | 是   | 表示创建订阅者的回调方法。 |
214
215**示例:**
216
217
218```ts
219import Base from '@ohos.base';
220import CommonEventManager from '@ohos.commonEventManager';
221
222let subscriber:CommonEventManager.CommonEventSubscriber; // 用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作
223
224// 订阅者信息
225let subscribeInfo:CommonEventManager.CommonEventSubscribeInfo = {
226    events: ["event"]
227};
228
229// 创建订阅者回调
230function createCB(err:Base.BusinessError, commonEventSubscriber:CommonEventManager.CommonEventSubscriber) {
231    if (err.code) {
232        console.error(`createSubscriber failed, code is ${err.code}`);
233    } else {
234        console.info("createSubscriber");
235        subscriber = commonEventSubscriber;
236    }
237}
238
239// 创建订阅者
240CommonEvent.createSubscriber(subscribeInfo, createCB);
241```
242
243## CommonEvent.createSubscriber<sup>(deprecated)</sup>
244
245createSubscriber(subscribeInfo: CommonEventSubscribeInfo): Promise\<CommonEventSubscriber>
246
247以Promise形式创建订阅者。
248
249> **说明:**
250>从 API version 7开始支持,从API version 9开始废弃。建议使用[commonEventManager.createSubscriber](js-apis-commonEventManager.md#commoneventmanagercreatesubscriber-1)替代。
251
252**系统能力:** `SystemCapability.Notification.CommonEvent`
253
254**参数:**
255
256| 参数名          | 类型                                                  | 必填 | 说明           |
257| ------------- | ----------------------------------------------------- | ---- | -------------- |
258| subscribeInfo | [CommonEventSubscribeInfo](./js-apis-inner-commonEvent-commonEventSubscribeInfo.md) | 是   | 表示订阅信息。 |
259
260**返回值:**
261| 类型                                                      | 说明             |
262| --------------------------------------------------------- | ---------------- |
263| Promise\<[CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md)> | 返回订阅者对象。 |
264
265**示例:**
266
267```ts
268import Base from '@ohos.base';
269import CommonEventManager from '@ohos.commonEventManager';
270
271let subscriber:CommonEventManager.CommonEventSubscriber; // 用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作
272
273// 订阅者信息
274let subscribeInfo:CommonEventManager.CommonEventSubscribeInfo = {
275    events: ["event"]
276};
277
278// 创建订阅者
279CommonEvent.createSubscriber(subscribeInfo).then((commonEventSubscriber:CommonEventManager.CommonEventSubscriber) => {
280    console.info("createSubscriber");
281    subscriber = commonEventSubscriber;
282}).catch((err:Base.BusinessError) => {
283    console.error(`createSubscriber failed, code is ${err.code}`);
284});
285```
286
287## CommonEvent.subscribe<sup>(deprecated)</sup>
288
289subscribe(subscriber: CommonEventSubscriber, callback: AsyncCallback\<CommonEventData>): void
290
291以回调形式订阅公共事件。
292
293> **说明:**
294>从 API version 7开始支持,从API version 9开始废弃。建议使用[commonEventManager.subscribe](js-apis-commonEventManager.md#commoneventmanagersubscribe)替代。
295
296**系统能力:** `SystemCapability.Notification.CommonEvent`
297
298**参数:**
299
300| 参数名       | 类型                                                | 必填 | 说明                             |
301| ---------- | ---------------------------------------------------- | ---- | -------------------------------- |
302| subscriber | [CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md)     | 是   | 表示订阅者对象。                 |
303| callback   | AsyncCallback\<[CommonEventData](./js-apis-inner-commonEvent-commonEventData.md)> | 是   | 表示接收公共事件数据的回调函数。 |
304
305**示例:**
306
307```ts
308import Base from '@ohos.base';
309import CommonEventManager from '@ohos.commonEventManager';
310
311let subscriber:CommonEventManager.CommonEventSubscriber;// 用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作
312
313// 订阅者信息
314let subscribeInfo:CommonEventManager.CommonEventSubscribeInfo = {
315    events: ["event"]
316};
317
318// 订阅公共事件回调
319function subscribeCB(err:Base.BusinessError, data:CommonEventManager.CommonEventData) {
320    if (err.code) {
321        console.error(`subscribe failed, code is ${err.code}`);
322    } else {
323        console.info("subscribe " + JSON.stringify(data));
324    }
325}
326
327// 创建订阅者回调
328function createCB(err:Base.BusinessError, commonEventSubscriber:CommonEventManager.CommonEventSubscriber) {
329    if (err.code) {
330        console.error(`createSubscriber failed, code is ${err.code}`);
331    } else {
332        console.info("createSubscriber");
333        subscriber = commonEventSubscriber;
334        // Subscribe to a common event.
335        CommonEvent.subscribe(subscriber, subscribeCB);
336    }
337}
338
339// 创建订阅者
340CommonEvent.createSubscriber(subscribeInfo, createCB);
341```
342
343## CommonEvent.unsubscribe<sup>(deprecated)</sup>
344
345unsubscribe(subscriber: CommonEventSubscriber, callback?: AsyncCallback\<void>): void
346
347以回调形式取消订阅公共事件。
348
349> **说明:**
350>从 API version 7开始支持,从API version 9开始废弃。建议使用[commonEventManager.subscribe](js-apis-commonEventManager.md#commoneventmanagerunsubscribe)替代。
351
352**系统能力:** `SystemCapability.Notification.CommonEvent`
353
354**参数:**
355
356| 参数名       | 类型                                             | 必填 | 说明                     |
357| ---------- | ----------------------------------------------- | ---- | ------------------------ |
358| subscriber | [CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md) | 是   | 表示订阅者对象。         |
359| callback   | AsyncCallback\<void>                            | 否   | 表示取消订阅的回调方法。 |
360
361**示例:**
362
363```ts
364import Base from '@ohos.base';
365import CommonEventManager from '@ohos.commonEventManager';
366
367let subscriber:CommonEventManager.CommonEventSubscriber;	// 用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作
368
369// 订阅者信息
370let subscribeInfo:CommonEventManager.CommonEventSubscribeInfo = {
371    events: ["event"]
372};
373
374// 订阅公共事件回调
375function subscribeCB(err:Base.BusinessError, data:CommonEventManager.CommonEventData) {
376    if (err.code) {
377        console.error(`subscribe failed, code is ${err.code}`);
378    } else {
379        console.info("subscribe " + JSON.stringify(data));
380    }
381}
382
383// 创建订阅者回调
384function createCB(err:Base.BusinessError, commonEventSubscriber:CommonEventManager.CommonEventSubscriber) {
385    if (err.code) {
386        console.error(`createSubscriber failed, code is ${err.code}`);
387    } else {
388        console.info("createSubscriber");
389        subscriber = commonEventSubscriber;
390        // Subscribe to a common event.
391        CommonEvent.subscribe(subscriber, subscribeCB);
392    }
393}
394
395// 取消订阅公共事件回调
396function unsubscribeCB(err:Base.BusinessError) {
397    if (err.code) {
398        console.error(`unsubscribe failed, code is ${err.code}`);
399    } else {
400        console.info("unsubscribe");
401    }
402}
403
404// 创建订阅者
405CommonEvent.createSubscriber(subscribeInfo, createCB);
406
407// 取消订阅公共事件
408CommonEvent.unsubscribe(subscriber, unsubscribeCB);
409```