@ohos.commonEvent (Common Event) (Deprecated)
The CommonEvent module provides capabilities to publish, subscribe to, and unsubscribe from common events, as well as obtain and modify the common event result code and result data.
NOTE
This API has been supported since API version 7 and deprecated since API version 9. You are advised to use @ohos.commonEventManager instead.
Modules to Import
import commonEvent from '@ohos.commonEvent';
Support
A 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.
For details about the definitions of all system common events, see System Common Events.
commonEvent.publish(deprecated)
publish(event: string, callback: AsyncCallback<void>): void
Publishes a common event. This API uses an asynchronous callback to return the result.
NOTE This API has been supported since API version 7 and deprecated since API version 9. You are advised to use commonEventManager.publish instead.
System capability: SystemCapability.Notification.CommonEvent
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| event | string | Yes | Name of the common event to publish. |
| callback | AsyncCallback<void> | Yes | Callback used to return the result. |
Example
import Base from '@ohos.base';
// Callback for common event publication.
function publishCB(err:Base.BusinessError) {
if (err.code) {
console.error(`publish failed, code is ${err.code}`);
} else {
console.info("publish");
}
}
// Publish a common event.
commonEvent.publish("event", publishCB);
commonEvent.publish(deprecated)
publish(event: string, options: CommonEventPublishData, callback: AsyncCallback<void>): void
Publishes a common event with given properties. This API uses an asynchronous callback to return the result.
NOTE This API has been supported since API version 7 and deprecated since API version 9. You are advised to use commonEventManager.publish instead.
System capability: SystemCapability.Notification.CommonEvent
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| event | string | Yes | Name of the common event to publish. |
| options | CommonEventPublishData | Yes | Properties of the common event to publish. |
| callback | AsyncCallback<void> | Yes | Callback used to return the result. |
Example
import Base from '@ohos.base';
import CommonEventManager from '@ohos.commonEventManager';
// Information of a common event.
let options:CommonEventManager.CommonEventPublishData = {
code: 0, // Initial code of the common event.
data: "initial data", // Initial data of the common event.
isOrdered: true // The common event is an ordered one.
}
// Callback for common event publication.
function publishCB(err:Base.BusinessError) {
if (err.code) {
console.error(`publish failed, code is ${err.code}`);
} else {
console.info("publish");
}
}
// Publish a common event.
commonEvent.publish("event", options, publishCB);
commonEvent.createSubscriber(deprecated)
createSubscriber(subscribeInfo: CommonEventSubscribeInfo, callback: AsyncCallback<CommonEventSubscriber>): void
Creates a subscriber. This API uses an asynchronous callback to return the result.
NOTE This API has been supported since API version 7 and deprecated since API version 9. You are advised to use commonEventManager.createSubscriber instead.
System capability: SystemCapability.Notification.CommonEvent
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| subscribeInfo | CommonEventSubscribeInfo | Yes | Subscriber information. |
| callback | AsyncCallback<CommonEventSubscriber> | Yes | Callback used to return the result. |
Example
import Base from '@ohos.base';
import CommonEventManager from '@ohos.commonEventManager';
let subscriber:CommonEventManager.CommonEventSubscriber; // Used to save the created subscriber object for subsequent subscription and unsubscription.
// Subscriber information.
let subscribeInfo:CommonEventManager.CommonEventSubscribeInfo = {
events: ["event"]
};
// Callback for subscriber creation.
function createCB(err:Base.BusinessError, commonEventSubscriber:CommonEventManager.CommonEventSubscriber) {
if (err.code) {
console.error(`createSubscriber failed, code is ${err.code}`);
} else {
console.info("createSubscriber");
subscriber = commonEventSubscriber;
}
}
// Create a subscriber.
commonEvent.createSubscriber(subscribeInfo, createCB);
commonEvent.createSubscriber(deprecated)
createSubscriber(subscribeInfo: CommonEventSubscribeInfo): Promise<CommonEventSubscriber>
Creates a subscriber. This API uses a promise to return the result.
NOTE This API has been supported since API version 7 and deprecated since API version 9. You are advised to use commonEventManager.createSubscriber instead.
System capability: SystemCapability.Notification.CommonEvent
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| subscribeInfo | CommonEventSubscribeInfo | Yes | Subscriber information. |
Return value
| Type | Description |
|---|---|
| Promise<CommonEventSubscriber> | Promise used to return the subscriber object. |
Example
import Base from '@ohos.base';
import CommonEventManager from '@ohos.commonEventManager';
let subscriber:CommonEventManager.CommonEventSubscriber; // Used to save the created subscriber object for subsequent subscription and unsubscription.
// Subscriber information.
let subscribeInfo:CommonEventManager.CommonEventSubscribeInfo = {
events: ["event"]
};
// Create a subscriber.
commonEvent.createSubscriber(subscribeInfo).then((commonEventSubscriber:CommonEventManager.CommonEventSubscriber) => {
console.info("createSubscriber");
subscriber = commonEventSubscriber;
}).catch((err:Base.BusinessError) => {
console.error(`createSubscriber failed, code is ${err.code}`);
});
commonEvent.subscribe(deprecated)
subscribe(subscriber: CommonEventSubscriber, callback: AsyncCallback<CommonEventData>): void
Subscribes to common events. This API uses an asynchronous callback to return the result.
NOTE This API has been supported since API version 7 and deprecated since API version 9. You are advised to use commonEventManager.subscribe instead.
System capability: SystemCapability.Notification.CommonEvent
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| subscriber | CommonEventSubscriber | Yes | Subscriber object. |
| callback | AsyncCallback<CommonEventData> | Yes | Callback used to return the result. |
Example
import Base from '@ohos.base';
import CommonEventManager from '@ohos.commonEventManager';
let subscriber:CommonEventManager.CommonEventSubscriber;// Used to save the created subscriber object for subsequent subscription and unsubscription.
// Subscriber information.
let subscribeInfo:CommonEventManager.CommonEventSubscribeInfo = {
events: ["event"]
};
// Callback for common event subscription.
function subscribeCB(err:Base.BusinessError, data:CommonEventManager.CommonEventData) {
if (err.code) {
console.error(`subscribe failed, code is ${err.code}`);
} else {
console.info("subscribe " + JSON.stringify(data));
}
}
// Callback for subscriber creation.
function createCB(err:Base.BusinessError, commonEventSubscriber:CommonEventManager.CommonEventSubscriber) {
if (err.code) {
console.error(`createSubscriber failed, code is ${err.code}`);
} else {
console.info("createSubscriber");
subscriber = commonEventSubscriber;
// Subscribe to a common event.
commonEvent.subscribe(subscriber, subscribeCB);
}
}
// Create a subscriber.
commonEvent.createSubscriber(subscribeInfo, createCB);
commonEvent.unsubscribe(deprecated)
unsubscribe(subscriber: CommonEventSubscriber, callback?: AsyncCallback<void>): void
Unsubscribes from common events. This API uses an asynchronous callback to return the result.
NOTE This API has been supported since API version 7 and deprecated since API version 9. You are advised to use commonEventManager.unsubscribe instead.
System capability: SystemCapability.Notification.CommonEvent
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| subscriber | CommonEventSubscriber | Yes | Subscriber object. |
| callback | AsyncCallback<void> | No | Callback used to return the result. |
Example
import Base from '@ohos.base';
import CommonEventManager from '@ohos.commonEventManager';
let subscriber:CommonEventManager.CommonEventSubscriber; // Used to save the created subscriber object for subsequent subscription and unsubscription.
// Subscriber information.
let subscribeInfo:CommonEventManager.CommonEventSubscribeInfo = {
events: ["event"]
};
// Callback for common event subscription.
function subscribeCB(err:Base.BusinessError, data:CommonEventManager.CommonEventData) {
if (err.code) {
console.error(`subscribe failed, code is ${err.code}`);
} else {
console.info("subscribe " + JSON.stringify(data));
}
}
// Callback for subscriber creation.
function createCB(err:Base.BusinessError, commonEventSubscriber:CommonEventManager.CommonEventSubscriber) {
if (err.code) {
console.error(`createSubscriber failed, code is ${err.code}`);
} else {
console.info("createSubscriber");
subscriber = commonEventSubscriber;
// Subscribe to a common event.
commonEvent.subscribe(subscriber, subscribeCB);
}
}
// Callback for common event unsubscription.
function unsubscribeCB(err:Base.BusinessError) {
if (err.code) {
console.error(`unsubscribe failed, code is ${err.code}`);
} else {
console.info("unsubscribe");
}
}
// Create a subscriber.
commonEvent.createSubscriber(subscribeInfo, createCB);
// Unsubscribe from the common event.
commonEvent.unsubscribe(subscriber, unsubscribeCB);