• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.commonEvent (Common Event)
2
3The **CommonEvent** module provides common event capabilities, including the capabilities to publish, subscribe to, and unsubscribe from common events, as well obtaining and setting the common event result code and result data.
4
5> **NOTE**
6>
7> - The APIs provided by this module are no longer maintained since API version 9. You are advised to use [@ohos.commonEventManager](js-apis-commonEventManager.md).
8>
9> - The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.
10
11## Modules to Import
12
13```ts
14import CommonEvent from '@ohos.commonEvent';
15```
16
17## Support
18
19A system common event is an event that is published by a system service or system application and requires specific permissions to subscribe to. To publish or subscribe to this type of event, you must follow the event-specific definitions.
20
21For details about the definitions of all system common events, see [System Common Events](./commonEvent-definitions.md).
22
23## CommonEvent.publish<sup>(deprecated)</sup>
24
25publish(event: string, callback: AsyncCallback\<void>): void
26
27Publishes a common event. This API uses an asynchronous callback to return the result.
28
29> **NOTE**
30>
31> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [commonEventManager.publish](js-apis-commonEventManager.md#commoneventmanagerpublish) instead.
32
33**System capability**: SystemCapability.Notification.CommonEvent
34
35**Parameters**
36
37| Name    | Type                | Mandatory| Description                  |
38| -------- | -------------------- | ---- | ---------------------- |
39| event    | string               | Yes  | Name of the common event to publish.|
40| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.|
41
42**Example**
43
44```ts
45import Base from '@ohos.base';
46
47// Callback for common event publication
48function publishCB(err:Base.BusinessError) {
49	if (err.code) {
50        console.error(`publish failed, code is ${err.code}`);
51    } else {
52        console.info("publish");
53    }
54}
55
56// Publish a common event.
57CommonEvent.publish("event", publishCB);
58```
59
60## CommonEvent.publish<sup>(deprecated)</sup>
61
62publish(event: string, options: CommonEventPublishData, callback: AsyncCallback\<void>): void
63
64Publishes a common event with given attributes. This API uses an asynchronous callback to return the result.
65
66> **NOTE**
67>
68> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [commonEventManager.publish](js-apis-commonEventManager.md#commoneventmanagerpublish-1) instead.
69
70**System capability**: SystemCapability.Notification.CommonEvent
71
72**Parameters**
73
74| Name    | Type                  | Mandatory| Description                  |
75| -------- | ---------------------- | ---- | ---------------------- |
76| event    | string                 | Yes  | Name of the common event to publish. |
77| options  | [CommonEventPublishData](./js-apis-inner-commonEvent-commonEventPublishData.md) | Yes  | Attributes of the common event to publish.|
78| callback | AsyncCallback\<void>   | Yes  | Callback used to return the result. |
79
80**Example**
81
82
83```ts
84import Base from '@ohos.base';
85import CommonEventManager from '@ohos.commonEventManager';
86
87// Attributes of a common event.
88let options:CommonEventManager.CommonEventPublishData = {
89	code: 0,			 // Result code of the common event.
90	data: "initial data";// Result data of the common event.
91	isOrdered: true	 // The common event is an ordered one.
92}
93
94// Callback for common event publication
95function publishCB(err:Base.BusinessError) {
96	if (err.code) {
97        console.error(`publish failed, code is ${err.code}`);
98    } else {
99        console.info("publish");
100    }
101}
102
103// Publish a common event.
104CommonEvent.publish("event", options, publishCB);
105```
106
107## CommonEvent.createSubscriber<sup>(deprecated)</sup>
108
109createSubscriber(subscribeInfo: CommonEventSubscribeInfo, callback: AsyncCallback\<CommonEventSubscriber>): void
110
111Creates a subscriber. This API uses an asynchronous callback to return the result.
112
113> **NOTE**
114>
115>This API is supported since API version 7 and deprecated since API version 9. You are advised to use [commonEventManager.createSubscriber](js-apis-commonEventManager.md#commoneventmanagercreatesubscriber) instead.
116
117**System capability**: SystemCapability.Notification.CommonEvent
118
119**Parameters**
120
121| Name         | Type                                                        | Mandatory| Description                      |
122| ------------- | ------------------------------------------------------------ | ---- | -------------------------- |
123| subscribeInfo | [CommonEventSubscribeInfo](./js-apis-inner-commonEvent-commonEventSubscribeInfo.md)        | Yes  | Subscriber information.            |
124| callback      | AsyncCallback\<[CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md)> | Yes  | Callback used to return the result.|
125
126**Example**
127
128
129```ts
130import Base from '@ohos.base';
131import CommonEventManager from '@ohos.commonEventManager';
132
133let subscriber:CommonEventManager.CommonEventSubscriber; // Used to save the created subscriber object for subsequent subscription and unsubscription.
134
135// Subscriber information.
136let subscribeInfo:CommonEventManager.CommonEventSubscribeInfo = {
137    events: ["event"]
138};
139
140// Callback for subscriber creation.
141function createCB(err:Base.BusinessError, commonEventSubscriber:CommonEventManager.CommonEventSubscriber) {
142    if (err.code) {
143        console.error(`createSubscriber failed, code is ${err.code}`);
144    } else {
145        console.info("createSubscriber");
146        subscriber = commonEventSubscriber;
147    }
148}
149
150// Create a subscriber.
151CommonEvent.createSubscriber(subscribeInfo, createCB);
152```
153
154## CommonEvent.createSubscriber<sup>(deprecated)</sup>
155
156createSubscriber(subscribeInfo: CommonEventSubscribeInfo): Promise\<CommonEventSubscriber>
157
158Creates a subscriber. This API uses a promise to return the result.
159
160> **NOTE**
161>
162>This API is supported since API version 7 and deprecated since API version 9. You are advised to use [commonEventManager.createSubscriber](js-apis-commonEventManager.md#commoneventmanagercreatesubscriber-1) instead.
163
164**System capability**: SystemCapability.Notification.CommonEvent
165
166**Parameters**
167
168| Name         | Type                                                 | Mandatory| Description          |
169| ------------- | ----------------------------------------------------- | ---- | -------------- |
170| subscribeInfo | [CommonEventSubscribeInfo](./js-apis-inner-commonEvent-commonEventSubscribeInfo.md) | Yes  | Subscriber information.|
171
172**Return value**
173| Type                                                     | Description            |
174| --------------------------------------------------------- | ---------------- |
175| Promise\<[CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md)> | Promise used to return the subscriber object.|
176
177**Example**
178
179```ts
180import Base from '@ohos.base';
181import CommonEventManager from '@ohos.commonEventManager';
182
183let subscriber:CommonEventManager.CommonEventSubscriber; // Used to save the created subscriber object for subsequent subscription and unsubscription.
184
185// Subscriber information.
186let subscribeInfo:CommonEventManager.CommonEventSubscribeInfo = {
187    events: ["event"]
188};
189
190// Create a subscriber.
191CommonEvent.createSubscriber(subscribeInfo).then((commonEventSubscriber:CommonEventManager.CommonEventSubscriber) => {
192    console.info("createSubscriber");
193    subscriber = commonEventSubscriber;
194}).catch((err:Base.BusinessError) => {
195    console.error(`createSubscriber failed, code is ${err.code}`);
196});
197```
198
199## CommonEvent.subscribe<sup>(deprecated)</sup>
200
201subscribe(subscriber: CommonEventSubscriber, callback: AsyncCallback\<CommonEventData>): void
202
203Subscribes to common events. This API uses an asynchronous callback to return the result.
204
205> **NOTE**
206>
207>This API is supported since API version 7 and deprecated since API version 9. You are advised to use [commonEventManager.subscribe](js-apis-commonEventManager.md#commoneventmanagersubscribe) instead.
208
209**System capability**: SystemCapability.Notification.CommonEvent
210
211**Parameters**
212
213| Name      | Type                                               | Mandatory| Description                            |
214| ---------- | ---------------------------------------------------- | ---- | -------------------------------- |
215| subscriber | [CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md)     | Yes  | Subscriber object.                |
216| callback   | AsyncCallback\<[CommonEventData](./js-apis-inner-commonEvent-commonEventData.md)> | Yes  | Callback used to return the result.|
217
218**Example**
219
220```ts
221import Base from '@ohos.base';
222import CommonEventManager from '@ohos.commonEventManager';
223
224let subscriber:CommonEventManager.CommonEventSubscriber;// Used to save the created subscriber object for subsequent subscription and unsubscription.
225
226// Subscriber information.
227let subscribeInfo:CommonEventManager.CommonEventSubscribeInfo = {
228    events: ["event"]
229};
230
231// Callback for common event subscription.
232function subscribeCB(err:Base.BusinessError, data:CommonEventManager.CommonEventData) {
233    if (err.code) {
234        console.error(`subscribe failed, code is ${err.code}`);
235    } else {
236        console.info("subscribe " + JSON.stringify(data));
237    }
238}
239
240// Callback for subscriber creation.
241function createCB(err:Base.BusinessError, commonEventSubscriber:CommonEventManager.CommonEventSubscriber) {
242    if (err.code) {
243        console.error(`createSubscriber failed, code is ${err.code}`);
244    } else {
245        console.info("createSubscriber");
246        subscriber = commonEventSubscriber;
247        // Subscribe to a common event.
248        CommonEvent.subscribe(subscriber, subscribeCB);
249    }
250}
251
252// Create a subscriber.
253CommonEvent.createSubscriber(subscribeInfo, createCB);
254```
255
256## CommonEvent.unsubscribe<sup>(deprecated)</sup>
257
258unsubscribe(subscriber: CommonEventSubscriber, callback?: AsyncCallback\<void>): void
259
260Unsubscribes from common events. This API uses an asynchronous callback to return the result.
261
262> **NOTE**
263>
264>This API is supported since API version 7 and deprecated since API version 9. You are advised to use [commonEventManager.subscribe](js-apis-commonEventManager.md#commoneventmanagerunsubscribe) instead.
265
266**System capability**: SystemCapability.Notification.CommonEvent
267
268**Parameters**
269
270| Name      | Type                                            | Mandatory| Description                    |
271| ---------- | ----------------------------------------------- | ---- | ------------------------ |
272| subscriber | [CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md) | Yes  | Subscriber object.        |
273| callback   | AsyncCallback\<void>                            | No  | Callback used to return the result.|
274
275**Example**
276
277```ts
278import Base from '@ohos.base';
279import CommonEventManager from '@ohos.commonEventManager';
280
281let subscriber:CommonEventManager.CommonEventSubscriber;	// Used to save the created subscriber object for subsequent subscription and unsubscription.
282
283// Subscriber information.
284let subscribeInfo:CommonEventManager.CommonEventSubscribeInfo = {
285    events: ["event"]
286};
287
288// Callback for common event subscription.
289function subscribeCB(err:Base.BusinessError, data:CommonEventManager.CommonEventData) {
290    if (err.code) {
291        console.error(`subscribe failed, code is ${err.code}`);
292    } else {
293        console.info("subscribe " + JSON.stringify(data));
294    }
295}
296
297// Callback for subscriber creation.
298function createCB(err:Base.BusinessError, commonEventSubscriber:CommonEventManager.CommonEventSubscriber) {
299    if (err.code) {
300        console.error(`createSubscriber failed, code is ${err.code}`);
301    } else {
302        console.info("createSubscriber");
303        subscriber = commonEventSubscriber;
304        // Subscribe to a common event.
305        CommonEvent.subscribe(subscriber, subscribeCB);
306    }
307}
308
309// Callback for common event unsubscription.
310function unsubscribeCB(err:Base.BusinessError) {
311    if (err.code) {
312        console.error(`unsubscribe failed, code is ${err.code}`);
313    } else {
314        console.info("unsubscribe");
315    }
316}
317
318// Create a subscriber.
319CommonEvent.createSubscriber(subscribeInfo, createCB);
320
321// Unsubscribe from the common event.
322CommonEvent.unsubscribe(subscriber, unsubscribeCB);
323```
324