Class Looper
prepareMainLooper()
in the thread that is to run the loop, and then
loop()
to have it process messages until the loop is stopped.
Most interaction with a message loop is through the
Handler
class.
Modified from Android Open Source Project.
-
Nested Class Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic Looper
Returns the application's main looper, which lives in the main thread of the application.getQueue()
Gets this looper's message queue.Gets the Thread associated with this Looper.boolean
Returns true if the current thread is this looper's thread.static void
loop()
Run the message queue in this thread.static Looper
myLooper()
Return the Looper object associated with the current thread.static MessageQueue
myQueue()
Return theMessageQueue
object associated with the current thread.static Looper
prepare()
Initialize the current thread as a looper.static void
Prepare the main event loop.void
quit()
Quits the looper.void
Quits the looper safely.static void
setObserver
(Looper.Observer observer) Set the transaction observer for all Loopers in this process.void
setSlowLogThresholdMs
(long slowDispatchThresholdMs, long slowDeliveryThresholdMs) Set a thresholds for slow dispatch/delivery log.toString()
-
Method Details
-
prepare
Initialize the current thread as a looper.This gives you a chance to create handlers that then reference this looper, before actually starting the loop. Be sure to call
loop()
after calling this method, and end it by callingquit()
.- Throws:
RuntimeException
- initializes twice
-
prepareMainLooper
Prepare the main event loop. This must be called from the entry point of the application. -
getMainLooper
Returns the application's main looper, which lives in the main thread of the application. -
setObserver
Set the transaction observer for all Loopers in this process. -
loop
public static void loop()Run the message queue in this thread. Be sure to callquit()
to end the loop. -
myLooper
Return the Looper object associated with the current thread. Returns null if the calling thread is not associated with a Looper. -
myQueue
Return theMessageQueue
object associated with the current thread.- Throws:
NullPointerException
- not called from a thread running a Looper
-
isCurrentThread
public boolean isCurrentThread()Returns true if the current thread is this looper's thread. -
setSlowLogThresholdMs
public void setSlowLogThresholdMs(long slowDispatchThresholdMs, long slowDeliveryThresholdMs) Set a thresholds for slow dispatch/delivery log. -
quit
public void quit()Quits the looper.Causes the
loop()
method to terminate without processing any more messages in the message queue.Any attempt to post messages to the queue after the looper is asked to quit will fail. For example, the
Handler.sendMessage(Message)
method will return false.Using this method may be unsafe because some messages may not be delivered before the looper terminates. Consider using
quitSafely()
instead to ensure that all pending work is completed in an orderly manner.- See Also:
-
quitSafely
public void quitSafely()Quits the looper safely.Causes the
loop()
method to terminate as soon as all remaining messages in the message queue that are already due to be delivered have been handled. However pending delayed messages with due times in the future will not be delivered before the loop terminates.Any attempt to post messages to the queue after the looper is asked to quit will fail. For example, the
Handler.sendMessage(Message)
method will return false. -
getThread
Gets the Thread associated with this Looper.- Returns:
- The looper's thread.
-
getQueue
Gets this looper's message queue.- Returns:
- The looper's message queue.
-
toString
-