public final class CommonEventManager extends Object
Upon completing an action, such as an upgrade, an application publishes a message to notify other applications or services of that action. The applications or services that received the message will complete the corresponding operation as indicated by the message. The application also subscribes to specified messages. After an event source publishes a message, the application can receive the subscribed message and perform the next operation, but the applications or services that have not subscribed to the message cannot receive it.
There are generally two types of common event types: standard and ordered. Standard common events are executed asynchronously. Specifically, subscribers involved will concurrently receive a published standard common event and the publishing cannot be interrupted. Ordered common events are executed synchronously. Specifically, subscribers involved receive the ordered common event sequentially. A subscriber can receive the ordered common event only after the previous subscriber finishes processing it. Therefore, ordered common event publishing can be interrupted.
The methods provided by this class are all static and can be called without creating an object of this class.
SystemCapability.Notification#CES| 限定符和类型 | 方法 | 说明 |
|---|---|---|
static void |
publishCommonEvent(CommonEventData eventData) |
Publishes a standard common event.
|
static void |
publishCommonEvent(CommonEventData eventData,
CommonEventPublishInfo publishInfo) |
Publishes an ordered, sticky, or standard common event.
|
static void |
publishCommonEvent(CommonEventData eventData,
CommonEventPublishInfo publishInfo,
CommonEventSubscriber resultSubscriber) |
Publishes an ordered, sticky, or standard common event.
|
static void |
subscribeCommonEvent(CommonEventSubscriber subscriber) |
Subscribes to common events.
|
static void |
unsubscribeCommonEvent(CommonEventSubscriber subscriber) |
Unsubscribes from common events.
|
public static void publishCommonEvent(CommonEventData eventData) throws RemoteException
A standard common event can be executed asynchronously and its publishing will not be interrupted.
All subscribers involved will receive the common event at the same time, but they will not return
any message. If you want the subscribers to return messages upon receiving a common event, call the
publishCommonEvent(ohos.event.commonevent.CommonEventData,ohos.event.commonevent.CommonEventPublishInfo,ohos.event.commonevent.CommonEventSubscriber) method.
If you want subscribers to receive messages in an ordered way, call the
publishCommonEvent(ohos.event.commonevent.CommonEventData,ohos.event.commonevent.CommonEventPublishInfo) or
publishCommonEvent(ohos.event.commonevent.CommonEventData,ohos.event.commonevent.CommonEventPublishInfo,ohos.event.commonevent.CommonEventSubscriber) method.
eventData - Indicates the CommonEventData object containing the content of
the standard common event. This parameter is mandatory.RemoteException - Throws this exception if an inter-process communication (IPC) exception occurs.IllegalArgumentException - Throws this exception if eventData is null.publishCommonEvent(CommonEventData, CommonEventPublishInfo),
publishCommonEvent(CommonEventData, CommonEventPublishInfo, CommonEventSubscriber)public static void publishCommonEvent(CommonEventData eventData, CommonEventPublishInfo publishInfo) throws RemoteException
To publish an ordered common event, call the CommonEventPublishInfo#setOrdered(boolean) method
with a value of true. To publish a sticky common event, call the
CommonEventPublishInfo#setSticky(boolean) method with a value of true. You must obtain the
ohos.permission.COMMONEVENT_STICKY permission before publishing a sticky common event.
To publish a standard event, use this method without specifying the publishInfo parameter.
eventData - Indicates the CommonEventData containing the common event content.
This parameter is mandatory.publishInfo - Indicates the CommonEventPublishInfo object containing the common event attributes,
such as ordered or local. This parameter is optional.RemoteException - Throws this exception if an IPC exception occurs.IllegalArgumentException - Throws this exception if eventData is null.publishCommonEvent(CommonEventData),
publishCommonEvent(CommonEventData, CommonEventPublishInfo, CommonEventSubscriber)public static void publishCommonEvent(CommonEventData eventData, CommonEventPublishInfo publishInfo, CommonEventSubscriber resultSubscriber) throws RemoteException
To publish an ordered common event, call the CommonEventPublishInfo#setOrdered(boolean) method
with a value of true. To publish a sticky common event, call the
CommonEventPublishInfo#setSticky(boolean) method with a value of true. You must obtain the
ohos.permission.COMMONEVENT_STICKY permission before publishing a sticky common event.
To publish a standard event, use this method without specifying the publishInfo parameter.
You can specify the last CommonEventSubscriber object to receive an ordered common event
in the resultSubscriber parameter.
eventData - Indicates the CommonEventData containing the common event content.
This parameter is mandatory.publishInfo - Indicates the CommonEventPublishInfo object containing the common event attributes,
such as ordered or local. This parameter is optional.resultSubscriber - Indicates the last CommonEventSubscriber to receive the ordered common event.
Generally, the current application is specified as the last subscriber to receive its own common event.
This parameter is optional.RemoteException - Throws this exception if an IPC exception occurs.IllegalArgumentException - Throws this exception if eventData is null.publishCommonEvent(CommonEventData),
publishCommonEvent(CommonEventData, CommonEventPublishInfo)public static void subscribeCommonEvent(CommonEventSubscriber subscriber) throws RemoteException
Before subscribing to a common event, you need to create a child class of CommonEventSubscriber
and override the CommonEventSubscriber#onReceiveEvent(CommonEventData) method.
The received common event will be stored in CommonEventData.
subscriber - Indicates the CommonEventSubscriber to receive the common event.
This parameter is mandatory.RemoteException - Throws this exception if an IPC exception occurs.IllegalArgumentException - Throws this exception if subscriber is null.unsubscribeCommonEvent(CommonEventSubscriber)public static void unsubscribeCommonEvent(CommonEventSubscriber subscriber) throws RemoteException
Generally, you subscribe to a common event by calling the
subscribeCommonEvent(ohos.event.commonevent.CommonEventSubscriber) method. If you do not want your application to
receive the common event any longer, unsubscribe from the common event using this method.
You can unsubscribe from only those common events that your application has subscribed to.
subscriber - Indicates the CommonEventSubscriber to receive the common event.
This parameter is mandatory.RemoteException - Throws this exception if an IPC exception occurs.IllegalArgumentException - Throws this exception if subscriber is null.subscribeCommonEvent(CommonEventSubscriber)