Class Looper
- java.lang.Object
-
- android.os.Looper
-
public final class Looper extends Object
Class used to run a message loop for a thread. Threads by default do not have a message loop associated with them; to create one, callprepare()in the thread that is to run the loop, and thenloop()to have it process messages until the loop is stopped.Most interaction with a message loop is through the
Handlerclass.This is a typical example of the implementation of a Looper thread, using the separation of
prepare()andloop()to create an initial Handler to communicate with the Looper.class LooperThread extends Thread { public Handler mHandler; public void run() { Looper.prepare(); mHandler = new Handler() { public void handleMessage(Message msg) { // process incoming messages here } }; Looper.loop(); } }
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voiddump(Printer pw, String prefix)Dumps the state of the looper for debugging purposes.static LoopergetMainLooper()Returns the application's main looper, which lives in the main thread of the application.MessageQueuegetQueue()Gets this looper's message queue.ThreadgetThread()Gets the Thread associated with this Looper.booleanisCurrentThread()Returns true if the current thread is this looper's thread.static voidloop()Run the message queue in this thread.static LoopermyLooper()Return the Looper object associated with the current thread.static MessageQueuemyQueue()Return theMessageQueueobject associated with the current thread.static voidprepare()Initialize the current thread as a looper.static voidprepareMainLooper()Initialize the current thread as a looper, marking it as an application's main looper.voidquit()Quits the looper.voidquitSafely()Quits the looper safely.voidsetMessageLogging(Printer printer)Control logging of messages as they are processed by this Looper.StringtoString()Returns a string containing a concise, human-readable description of this object.
-
-
-
Method Detail
-
prepare
public static void prepare()
-
prepareMainLooper
public static void prepareMainLooper()
Initialize the current thread as a looper, marking it as an application's main looper. The main looper for your application is created by the Android environment, so you should never need to call this function yourself. See also:prepare()
-
getMainLooper
public static Looper getMainLooper()
Returns the application's main looper, which lives in the main thread of the application.
-
loop
public static void loop()
Run the message queue in this thread. Be sure to callquit()to end the loop.
-
myLooper
public static Looper myLooper()
Return the Looper object associated with the current thread. Returns null if the calling thread is not associated with a Looper.
-
myQueue
public static MessageQueue myQueue()
Return theMessageQueueobject associated with the current thread. This must be called from a thread running a Looper, or a NullPointerException will be thrown.
-
isCurrentThread
public boolean isCurrentThread()
Returns true if the current thread is this looper's thread.
-
setMessageLogging
public void setMessageLogging(Printer printer)
Control logging of messages as they are processed by this Looper. If enabled, a log message will be written to printer at the beginning and ending of each message dispatch, identifying the target Handler and message contents.- Parameters:
printer- A Printer object that will receive log messages, or null to disable message logging.
-
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()
-
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
public Thread getThread()
Gets the Thread associated with this Looper.- Returns:
- The looper's thread.
-
getQueue
public MessageQueue getQueue()
Gets this looper's message queue.- Returns:
- The looper's message queue.
-
dump
public void dump(Printer pw, String prefix)
Dumps the state of the looper for debugging purposes.- Parameters:
pw- A printer to receive the contents of the dump.prefix- A prefix to prepend to each line which is printed.
-
toString
public String toString()
Description copied from class:ObjectReturns a string containing a concise, human-readable description of this object. Subclasses are encouraged to override this method and provide an implementation that takes into account the object's type and data. The default implementation is equivalent to the following expression:getClass().getName() + '@' + Integer.toHexString(hashCode())
See Writing a useful
toStringmethod if you intend implementing your owntoStringmethod.
-
-