Class MessageQueue
Looper
. Messages are not added directly to a MessageQueue,
but rather through Handler
objects associated with the Looper.
You can retrieve the MessageQueue for the current thread with
Looper.myQueue()
.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
Callback interface for discovering when a thread is going to block waiting for more messages. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addIdleHandler
(MessageQueue.IdleHandler handler) Add a newMessageQueue.IdleHandler
to this message queue.boolean
isIdle()
Returns true if the looper has no pending messages which are due to be processed.boolean
Returns whether this looper's thread is currently polling for more work to do.int
Posts a synchronization barrier to the Looper's message queue.void
Remove anMessageQueue.IdleHandler
from the queue that was previously added withaddIdleHandler(icyllis.modernui.core.MessageQueue.IdleHandler)
.void
removeSyncBarrier
(int token) Removes a synchronization barrier.
-
Method Details
-
isIdle
public boolean isIdle()Returns true if the looper has no pending messages which are due to be processed.This method is safe to call from any thread.
- Returns:
- True if the looper is idle.
-
addIdleHandler
Add a newMessageQueue.IdleHandler
to this message queue. This may be removed automatically for you by returning false fromIdleHandler.queueIdle()
when it is invoked, or explicitly removing it withremoveIdleHandler(icyllis.modernui.core.MessageQueue.IdleHandler)
.This method is safe to call from any thread.
- Parameters:
handler
- The IdleHandler to be added.
-
removeIdleHandler
Remove anMessageQueue.IdleHandler
from the queue that was previously added withaddIdleHandler(icyllis.modernui.core.MessageQueue.IdleHandler)
. If the given object is not currently in the idle list, nothing is done.This method is safe to call from any thread.
- Parameters:
handler
- The IdleHandler to be removed.
-
isPolling
public boolean isPolling()Returns whether this looper's thread is currently polling for more work to do. This is a good signal that the loop is still alive rather than being stuck handling a callback. Note that this method is intrinsically racy, since the state of the loop can change before you get the result back.This method is safe to call from any thread.
- Returns:
- True if the looper is currently polling for events.
-
postSyncBarrier
public int postSyncBarrier()Posts a synchronization barrier to the Looper's message queue.Message processing occurs as usual until the message queue encounters the synchronization barrier that has been posted. When the barrier is encountered, later synchronous messages in the queue are stalled (prevented from being executed) until the barrier is released by calling
removeSyncBarrier(int)
and specifying the token that identifies the synchronization barrier.This method is used to immediately postpone execution of all subsequently posted synchronous messages until a condition is met that releases the barrier. Asynchronous messages (see
Message.isAsynchronous()
) are exempt from the barrier and continue to be processed as usual.This call must be always matched by a call to
removeSyncBarrier(int)
with the same token to ensure that the message queue resumes normal operation. Otherwise, the application will probably hang!- Returns:
- A token that uniquely identifies the barrier. This token must be
passed to
removeSyncBarrier(int)
to release the barrier.
-
removeSyncBarrier
public void removeSyncBarrier(int token) Removes a synchronization barrier.- Parameters:
token
- The synchronization barrier token that was returned bypostSyncBarrier()
.- Throws:
IllegalStateException
- if the barrier was not found.
-