public abstract class CommonEventSubscriber extends Object
CommonEventSubscriber is an abstract class. If you want your application to receive
common events from other applications, you must first inherit this class and implement
the onReceiveEvent(ohos.event.commonevent.CommonEventData) method. Then, you can use methods provided
in CommonEventManager to subscribe to or unsubscribe from certain common events.
The received common events will be stored in CommonEventData.
| 构造器 | 说明 |
|---|---|
CommonEventSubscriber(CommonEventSubscribeInfo subscribeInfo) |
A constructor used to create a
CommonEventSubscriber instance with
the subscribeInfo parameter passed. |
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
void |
abortCommonEvent() |
Aborts the current ordered common event.
|
void |
clearAbortCommonEvent() |
Clears the abort state of the current ordered common event.
|
boolean |
getAbortCommonEvent() |
Checks whether the current ordered common event should be aborted.
|
int |
getCode() |
Obtains the result code of the current ordered common event.
|
String |
getData() |
Obtains the result data of the current ordered common event.
|
IRemoteObject |
getRemoteAbility(Context zContext,
Intent zIntent) |
Obtains an
IRemoteObject object bound to a specified ability. |
CommonEventSubscribeInfo |
getSubscribeInfo() |
Obtains the current
CommonEventSubscribeInfo object, which is passed in the constructor. |
AsyncCommonEventResult |
goAsyncCommonEvent() |
Enables asynchronous processing for the current ordered common event.
|
boolean |
isOrderedCommonEvent() |
Checks whether the current common event is an ordered common event.
|
boolean |
isStickyCommonEvent() |
Checks whether the current common event is a sticky common event.
|
abstract void |
onReceiveEvent(CommonEventData data) |
Called back when the application receives a new common event.
|
void |
setCode(int code) |
Sets the result code of the current ordered common event.
|
void |
setCodeAndData(int code,
String data) |
Sets the result of the current ordered common event.
|
void |
setData(String data) |
Sets the result data of the current ordered common event.
|
String |
toString() |
Returns a string representation of the object.
|
public CommonEventSubscriber(CommonEventSubscribeInfo subscribeInfo)
CommonEventSubscriber instance with
the subscribeInfo parameter passed.subscribeInfo - Indicates the CommonEventSubscribeInfo object to set.IllegalArgumentException - Throws this exception if subscribeInfo is null.public abstract void onReceiveEvent(CommonEventData data)
If you want to subscribe to and receive common events from other applications,
you should inherit the CommonEventSubscriber class and override this method.
data - Indicates the received CommonEventData object.public CommonEventSubscribeInfo getSubscribeInfo()
CommonEventSubscribeInfo object, which is passed in the constructor.CommonEventSubscribeInfo object.public final AsyncCommonEventResult goAsyncCommonEvent()
This method is called using the onReceiveEvent(ohos.event.commonevent.CommonEventData) callback method.
The common event remains active after being returned from onReceiveEvent(ohos.event.commonevent.CommonEventData).
That is, the common event can continue to perform its time-consuming operations on another thread instead
of the main thread. However, this does not mean that time-consuming operations can be executed by calling
the callback method. In addition, your application is required to call
AsyncCommonEventResult#finishCommonEvent() to finish processing the ordered common event before
it is complete.
public final void setCode(int code)
This method takes effect only for ordered common events.
You can call CommonEventPublishInfo#setOrdered(boolean) with the input parameter set to true
to set a common event ordered. The next subscriber will receive the result code after the code is set.
code - Indicates the custom result code to set. You can set it to any int value.public final int getCode()
public final void setData(String data)
This method takes effect only for ordered common events.
You can call CommonEventPublishInfo#setOrdered(boolean) with the input parameter set to true
to set a common event ordered.
data - Indicates the custom result data to set. You can set it to any character string.public final String getData()
null.public final void setCodeAndData(int code,
String data)
This method takes effect only for ordered common events.
You can call CommonEventPublishInfo#setOrdered(boolean) with the input parameter set to true
to set a common event ordered.
code - Indicates the custom result code to set. You can set it to any int value.data - Indicates the custom result data to set. You can set it to any character string.public final void abortCommonEvent()
This method takes effect only for ordered common events. Once this method is called, no subscribers will
receive this common event. You can call clearAbortCommonEvent() to revoke this operation.
public final boolean getAbortCommonEvent()
true if the current common event should be aborted; returns false otherwise.public final void clearAbortCommonEvent()
getAbortCommonEvent() will return false.public IRemoteObject getRemoteAbility(Context zContext, Intent zIntent) throws RemoteException
IRemoteObject object bound to a specified ability.
The ability must have been started via
Ability.connectAbility(Intent, IAbilityConnection) before this method is called.
You are advised to call this method in
onReceiveEvent(CommonEventData)
to obtain an IRemoteObject object that is bound to a started ability.zContext - Indicates the Context object of the current application.zIntent - Indicates information about the ability that has been started.IRemoteObject bound to the specified ability.RemoteException - Throws this exception if an IPC exception occurs.public final boolean isOrderedCommonEvent()
true if it is an ordered common event; returns false otherwise.public final boolean isStickyCommonEvent()
true if it is a sticky common event; returns false otherwise.public String toString()
ObjecttoString method returns a string that
"textually represents" this object. The result should
be a concise but informative representation that is easy for a
person to read.
It is recommended that all subclasses override this method.
The toString method for class Object
returns a string consisting of the name of the class of which the
object is an instance, the at-sign character `@', and
the unsigned hexadecimal representation of the hash code of the
object. In other words, this method returns a string equal to the
value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())