public class EventHandler extends Object
InnerEvent and Runnable objects on asynchronous threads.
Each EventHandler instance is bound to a thread, and the thread has an event queue.
The thread cyclically retrieves events from the event queue and dispatches the events to the EventHandler
for processing. The EventHandler delivers an event or a Runnable task to the thread's event queue
and executes the event or task when it comes out of the event queue.
You can use an EventHandler to dispatch and process events and Runnable objects among different
threads and to schedule events or Runnable objects to be processed at a certain interval.
You can use methods provided in this class to send a synchronous or asynchronous event, delay the processing of an event, and set the event priority.
You need to override the processEvent(ohos.eventhandler.InnerEvent) method to process events.
| 限定符和类型 | 类 | 说明 |
|---|---|---|
static class |
EventHandler.Priority |
Defines the priority of an
InnerEvent. |
| 构造器 | 说明 |
|---|---|
EventHandler(EventRunner runner) |
A constructor used to create an
EventHandler instance and bind it to an EventRunner. |
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
static EventHandler |
current() |
Obtains the current event handler.
|
void |
distributeEvent(InnerEvent event) |
Distributes a specified
InnerEvent. |
void |
dump(Dumper dumper) |
Prints out the internal information about an object in the specified format,
helping you diagnose internal errors of the object.
|
String |
getEventName(InnerEvent event) |
Obtains the name of a specified
InnerEvent. |
EventRunner |
getEventRunner() |
Obtains the
EventRunner bound to the current EventHandler. |
boolean |
hasInnerEvent(int eventId) |
Checks whether an event with the given ID can be found among the events that have been sent but not processed.
|
boolean |
hasInnerEvent(int eventId,
Object object) |
Checks whether an event with the given ID and the specified
object can be found among the events
that have been sent but not processed. |
boolean |
hasInnerEvent(long param) |
Checks whether an event carrying the given
param can be found among the events that
have been sent but not processed. |
boolean |
hasInnerEvent(Object object) |
Checks whether an event carrying the given
object can be found among the events that
have been sent but not processed. |
boolean |
hasInnerEvent(Runnable runnable) |
Checks whether the given
Runnable task can be found among the events that have been
sent but not processed. |
boolean |
isIdle() |
Checks whether the current
EventHandler is idle. |
void |
postSyncTask(Runnable task) |
Sends a synchronous
Runnable task to the event queue. |
void |
postSyncTask(Runnable task,
EventHandler.Priority priority) |
Sends a synchronous
Runnable task with a specified priority to the event queue. |
void |
postTask(Runnable task) |
Sends a
Runnable task to the event queue. |
void |
postTask(Runnable task,
long delayTime) |
Sends a delayed
Runnable task to the event queue. |
void |
postTask(Runnable task,
long delayTime,
EventHandler.Priority priority) |
Sends a delayed
Runnable task with a specified priority to the event queue. |
void |
postTask(Runnable task,
Object object) |
Sends a
Runnable task that carries the specified object to the event queue. |
void |
postTask(Runnable task,
EventHandler.Priority priority) |
Sends a
Runnable task with a specified priority to the event queue. |
void |
postTimingTask(Runnable task,
long taskTime) |
Sends a scheduled
Runnable task with the default priority EventHandler.Priority#LOW
to the event queue. |
void |
postTimingTask(Runnable task,
long taskTime,
EventHandler.Priority priority) |
Sends a scheduled
Runnable task with a specified priority to the event queue. |
void |
postTimingTask(Runnable task,
Object obj,
long taskTime) |
Sends a scheduled
Runnable task that carries the specified object to the event queue. |
protected void |
processEvent(InnerEvent event) |
Called for processing events.
|
void |
removeAllEvent() |
Removes all waiting events from the
EventHandler. |
void |
removeEvent(int eventId) |
Removes specified waiting events from the
EventHandler based on the given event ID. |
void |
removeEvent(int eventId,
long param) |
Removes specified waiting events from the
EventHandler based on the given event ID and param. |
void |
removeEvent(int eventId,
long param,
Object object) |
Removes specified waiting events from the
EventHandler based on the given event ID,
param, and object. |
void |
removeEvent(int eventId,
Object object) |
Removes specified waiting events from the
EventHandler based on the given event ID and object. |
void |
removeTask(Runnable task) |
Removes the given
Runnable task from the events that have been sent but not processed. |
void |
removeTask(Runnable task,
Object object) |
Removes the given
Runnable task that carries the specified object from the events that
have been sent but not processed. |
void |
sendEvent(int eventId) |
Sends an empty event (containing only the event ID) to the event queue.
|
void |
sendEvent(int eventId,
long delayTime) |
Sends a delayed empty event (containing only the event ID) to the event queue.
|
void |
sendEvent(int eventId,
long delayTime,
EventHandler.Priority priority) |
Sends a delayed empty event (containing only the event ID) with a specified priority to the event queue.
|
void |
sendEvent(int eventId,
EventHandler.Priority priority) |
Sends an empty event (containing only the event ID) with a specified priority to the event queue.
|
void |
sendEvent(InnerEvent event) |
Sends an event to the event queue.
|
void |
sendEvent(InnerEvent event,
long delayTime) |
Sends a delayed event to the event queue.
|
void |
sendEvent(InnerEvent event,
long delayTime,
EventHandler.Priority priority) |
Sends a delayed event with a specified priority to the event queue.
|
void |
sendEvent(InnerEvent event,
EventHandler.Priority priority) |
Sends an event with a specified priority to the event queue.
|
void |
sendSyncEvent(int eventId) |
Sends an empty synchronous event (containing only the event ID) to the event queue.
|
void |
sendSyncEvent(int eventId,
EventHandler.Priority priority) |
Sends an empty synchronous event (containing only the event ID) with a specified priority to the event queue.
|
void |
sendSyncEvent(InnerEvent event) |
Sends a synchronous event to the event queue.
|
void |
sendSyncEvent(InnerEvent event,
EventHandler.Priority priority) |
Sends a synchronous event with a specified priority to the event queue.
|
void |
sendTimingEvent(int eventId,
long taskTime) |
Sends a scheduled event containing only the event ID to the event queue.
|
void |
sendTimingEvent(int eventId,
long taskTime,
EventHandler.Priority priority) |
Sends a scheduled event containing only the event ID with a specified priority to the event queue.
|
void |
sendTimingEvent(InnerEvent event,
long taskTime) |
Sends a scheduled event with the default priority
EventHandler.Priority#LOW to the event queue. |
void |
sendTimingEvent(InnerEvent event,
long taskTime,
EventHandler.Priority priority) |
Sends a scheduled event with a specified priority to the event queue.
|
String |
toString() |
Returns a string representation of the object.
|
public EventHandler(EventRunner runner) throws IllegalArgumentException
EventHandler instance and bind it to an EventRunner.runner - Indicates the EventRunner instance running the EventHandler.IllegalArgumentException - Throws this exception if the given EventRunner is invalid.public static EventHandler current()
EventHandler instance.public void sendEvent(InnerEvent event, long delayTime, EventHandler.Priority priority) throws IllegalArgumentException
event - Indicates the InnerEvent to send.delayTime - Indicates the time period after which the event will be processed, in milliseconds. The value
must be greater than 0. Otherwise, the event will not be delayed.priority - Indicates the event priority. For details, see EventHandler.Priority.IllegalArgumentException - Throws this exception if an input parameter is invalid, for example,
if event is null or the InnerEvent instance indicated by event has been sent
by another EventHandler.public void sendEvent(InnerEvent event) throws IllegalArgumentException
event - Indicates the InnerEvent to send.IllegalArgumentException - Throws this exception if an input parameter is invalid, for example,
if event is null or the InnerEvent instance indicated by event has been sent
by another EventHandler.public void sendEvent(InnerEvent event, long delayTime) throws IllegalArgumentException
event - Indicates the InnerEvent to send.delayTime - Indicates the time period after which the event will be processed, in milliseconds. The value
must be greater than 0. Otherwise, the event will not be delayed.IllegalArgumentException - Throws this exception if an input parameter is invalid, for example,
if event is null or the InnerEvent instance indicated by event has been sent
by another EventHandler.public void sendEvent(InnerEvent event, EventHandler.Priority priority) throws IllegalArgumentException
event - Indicates the InnerEvent to send.priority - Indicates the event priority. For details, see EventHandler.Priority.IllegalArgumentException - Throws this exception if an input parameter is invalid, for example,
if event is null or the InnerEvent instance indicated by event has been sent
by another EventHandler.public void sendEvent(int eventId)
throws IllegalArgumentException
eventId - Indicates the ID of the event to send. You can set it to any int value.IllegalArgumentException - Throws this exception if the system fails to use eventId
to create an InnerEvent.public void sendEvent(int eventId,
long delayTime)
throws IllegalArgumentException
eventId - Indicates the ID of the event to send. You can set it to any int value.delayTime - Indicates the time period after which the event will be processed, in milliseconds. The value
must be greater than 0. Otherwise, the event will not be delayed.IllegalArgumentException - Throws this exception if the system fails to use eventId
to create an InnerEvent.public void sendEvent(int eventId,
EventHandler.Priority priority)
throws IllegalArgumentException
eventId - Indicates the ID of the event to send. You can set it to any int value.priority - Indicates the event priority. For details, see EventHandler.Priority.IllegalArgumentException - Throws this exception if the system fails to use eventId
to create an InnerEvent.public void sendEvent(int eventId,
long delayTime,
EventHandler.Priority priority)
throws IllegalArgumentException
eventId - Indicates the ID of the event to send. You can set it to any int value.delayTime - Indicates the time period after which the event will be processed, in milliseconds. The value
must be greater than 0. Otherwise, the event will not be delayed.priority - Indicates the event priority. For details, see EventHandler.Priority.IllegalArgumentException - Throws this exception if the system fails to use eventId
to create an InnerEvent.public void postTask(Runnable task, long delayTime, EventHandler.Priority priority) throws IllegalArgumentException
Runnable task with a specified priority to the event queue.task - Indicates the Runnable task to send.delayTime - Indicates the time period after which the task will be processed, in milliseconds. The value
must be greater than 0. Otherwise, the event will not be delayed.priority - Indicates the task priority. For details, see EventHandler.Priority.IllegalArgumentException - Throws this exception if an input parameter is invalid, for example,
if task is null.public void postTask(Runnable task) throws IllegalArgumentException
Runnable task to the event queue.task - Indicates the Runnable task to send.IllegalArgumentException - Throws this exception if an input parameter is invalid, for example,
if task is null.public void postTask(Runnable task, long delayTime) throws IllegalArgumentException
Runnable task to the event queue.task - Indicates the Runnable task to send.delayTime - Indicates the time period after which the task will be processed, in milliseconds. The value
must be greater than 0. Otherwise, the task will not be delayed.IllegalArgumentException - Throws this exception if an input parameter is invalid, for example,
if task is null.public void postTask(Runnable task, EventHandler.Priority priority) throws IllegalArgumentException
Runnable task with a specified priority to the event queue.task - Indicates the Runnable task to send.priority - Indicates the task priority. For details, see EventHandler.Priority.IllegalArgumentException - Throws this exception if an input parameter is invalid, for example,
if task is null.public void sendSyncEvent(InnerEvent event, EventHandler.Priority priority) throws IllegalArgumentException
event - Indicates the InnerEvent to send.priority - Indicates the event priority. For details, see EventHandler.Priority.IllegalArgumentException - Throws this exception if an input parameter is invalid, for example,
if event is null, the InnerEvent instance indicated by event has been sent
by another EventHandler, or priority is EventHandler.Priority#IDLE.public void sendSyncEvent(InnerEvent event) throws IllegalArgumentException
event - Indicates the InnerEvent to send.IllegalArgumentException - Throws this exception if an input parameter is invalid, for example,
if event is null or the InnerEvent instance indicated by event has been sent
by another EventHandler.public void sendSyncEvent(int eventId,
EventHandler.Priority priority)
throws IllegalArgumentException
eventId - Indicates the ID of the event to send. You can set it to any int value.priority - Indicates the event priority. For details, see EventHandler.Priority.IllegalArgumentException - Throws this exception if the system fails to use eventId
to create an InnerEvent or if priority is EventHandler.Priority#IDLE.public void sendSyncEvent(int eventId)
throws IllegalArgumentException
eventId - Indicates the ID of the event to send. You can set it to any int value.IllegalArgumentException - Throws this exception if the system fails to use eventId
to create an InnerEvent.public void postSyncTask(Runnable task, EventHandler.Priority priority) throws IllegalArgumentException
Runnable task with a specified priority to the event queue.
This thread is blocked until the task is executed.task - Indicates the Runnable task to send.priority - Indicates the task priority. For details, see EventHandler.Priority.IllegalArgumentException - Throws this exception if an input parameter is invalid, for example,
if task is null or priority is EventHandler.Priority#IDLE.public void postSyncTask(Runnable task) throws IllegalArgumentException
Runnable task to the event queue. This thread is blocked until the task is executed.task - Indicates the Runnable task to send.IllegalArgumentException - Throws this exception if an input parameter is invalid, for example,
if task is null.public void postTask(Runnable task, Object object) throws IllegalArgumentException
Runnable task that carries the specified object to the event queue.task - Indicates the Runnable task to send.object - Indicates the object carried in the task. This parameter can be null.IllegalArgumentException - Throws this exception if an input parameter is invalid, for example,
if task is null or the task has been sent by another EventHandler.public void sendTimingEvent(InnerEvent event, long taskTime, EventHandler.Priority priority) throws IllegalArgumentException
event - Indicates the InnerEvent to send.taskTime - Indicates the scheduled time to trigger the event, in ms. The value must be
greater than the current system runtime. Otherwise, the event will be triggered immediately.priority - Indicates the event priority. For details, see EventHandler.Priority.IllegalArgumentException - Throws this exception if an input parameter is invalid, for example,
if event is null or the InnerEvent instance indicated by event has been
sent by another EventHandler.public void sendTimingEvent(InnerEvent event, long taskTime) throws IllegalArgumentException
EventHandler.Priority#LOW to the event queue.event - Indicates the InnerEvent to send.taskTime - Indicates the scheduled time to trigger the event, in ms. The value must be
greater than the current system runtime. Otherwise, the event will be triggered immediately.IllegalArgumentException - Throws this exception if an input parameter is invalid, for example,
if event is null or the InnerEvent instance indicated by event has been
sent by another EventHandler.public void sendTimingEvent(int eventId,
long taskTime)
throws IllegalArgumentException
EventHandler.Priority#LOW.eventId - Indicates the ID of the event to send. You can set it to any int value.taskTime - Indicates the scheduled time to trigger the event, in ms. The value must be
greater than the current system runtime. Otherwise, the event will be triggered immediately.IllegalArgumentException - Throws this exception if the system fails to use eventId
to create an InnerEvent.public void sendTimingEvent(int eventId,
long taskTime,
EventHandler.Priority priority)
throws IllegalArgumentException
EventHandler.Priority#LOW.eventId - Indicates the ID of the event to send. You can set it to any int value.taskTime - Indicates the scheduled time to trigger the event, in ms. The value must be
greater than the current system runtime. Otherwise, the event will be triggered immediately.priority - Indicates the event priority. For details, see EventHandler.Priority.IllegalArgumentException - Throws this exception if the system fails to use eventId
to create an InnerEvent.public void postTimingTask(Runnable task, long taskTime, EventHandler.Priority priority) throws IllegalArgumentException
Runnable task with a specified priority to the event queue.task - Indicates the Runnable task to send.taskTime - Indicates the scheduled time to trigger the task, in ms. The value must be
greater than the current system runtime. Otherwise, the task will be triggered immediately.priority - Indicates the task priority. For details, see EventHandler.Priority.IllegalArgumentException - Throws this exception if an input parameter is invalid,
for example, if task is null.public void postTimingTask(Runnable task, Object obj, long taskTime) throws IllegalArgumentException
Runnable task that carries the specified object to the event queue.task - Indicates the Runnable task to send. This parameter cannot be null.obj - Indicates the object carried in the task. This parameter can be null.taskTime - Indicates the scheduled time to trigger the task, in ms. The value must be greater than the
current system runtime. Otherwise, the task will be triggered immediately.IllegalArgumentException - Throws this exception if an input parameter is invalid, for example,
if task is null.public void postTimingTask(Runnable task, long taskTime) throws IllegalArgumentException
Runnable task with the default priority EventHandler.Priority#LOW
to the event queue.task - Indicates the Runnable task to send.taskTime - Indicates the scheduled time to trigger the task, in ms. The value must be
greater than the current system runtime. Otherwise, the task will be triggered immediately.IllegalArgumentException - Throws this exception if an input parameter is invalid,
for example, if task is null.public void removeAllEvent()
EventHandler.
Waiting events are events that have been sent to the event queue but have not been processed.
public void removeEvent(int eventId)
EventHandler based on the given event ID.
Waiting events are events that have been sent to the event queue but have not been processed.
eventId - Indicates the event ID. All events with this ID will be removed.public void removeEvent(int eventId,
long param)
EventHandler based on the given event ID and param.
Waiting events are events that have been sent to the event queue but have not been processed.
eventId - Indicates the event ID.param - Indicates the parameter carried in the event.public void removeEvent(int eventId,
Object object)
EventHandler based on the given event ID and object.
Waiting events are events that have been sent to the event queue but have not been processed.
eventId - Indicates the event ID.object - Indicates the object carried in the event.public void removeEvent(int eventId,
long param,
Object object)
EventHandler based on the given event ID,
param, and object.
Waiting events are events that have been sent to the event queue but have not been processed.
eventId - Indicates the event ID.param - Indicates the parameter carried in the event.object - Indicates the object carried in the event.public void removeTask(Runnable task)
Runnable task from the events that have been sent but not processed.task - Indicates the Runnable task to remove.public void removeTask(Runnable task, Object object)
Runnable task that carries the specified object from the events that
have been sent but not processed.task - Indicates the Runnable task to remove.object - Indicates the object carried in the task. This parameter can be null.public final EventRunner getEventRunner()
EventRunner bound to the current EventHandler.
Each EventHandler must be bound to an EventRunner after being created.
EventRunner bound to the current EventHandler object.public final boolean isIdle()
EventHandler is idle.true if all events have been processed; returns false otherwise.public final boolean hasInnerEvent(Runnable runnable)
Runnable task can be found among the events that have been
sent but not processed.runnable - Indicates the Runnable task to find.true if the given Runnable task is found; returns
false otherwise.public final boolean hasInnerEvent(long param)
param can be found among the events that
have been sent but not processed.param - Indicates the long integer parameter carried in the event.true if the event carrying the given param is found; returns
false otherwise.public final boolean hasInnerEvent(Object object)
object can be found among the events that
have been sent but not processed.object - Indicates the object carried in the event.true if the event carrying the given object is found; returns
false otherwise.public final boolean hasInnerEvent(int eventId)
eventId - Indicates the ID of the event to find.true if the event with the given ID can be found; returns false otherwise.public final boolean hasInnerEvent(int eventId,
Object object)
object can be found among the events
that have been sent but not processed.eventId - Indicates the ID of the event to find.object - Indicates the object carried in the event. This parameter can be null.true if the event with the given ID and the specified object is found;
returns false otherwise.public final void dump(Dumper dumper)
dumper - Indicates the Dumper object you have implemented to
process the output internal information.public String getEventName(InnerEvent event)
InnerEvent.event - Indicates the InnerEvent whose name is to be obtained.public void distributeEvent(InnerEvent event)
InnerEvent.
If the InnerEvent carries a task, that is, its task attribute is not null,
the task will be executed.
If the InnerEvent does not carry any task, processEvent(ohos.eventhandler.InnerEvent) will
be called to process the event.
event - Indicates the InnerEvent to distribute.protected void processEvent(InnerEvent event)
You should override this method to process events sent by the EventHandler.
event - Indicates the InnerEvent to process.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())