public final class Looper extends Object
prepare()
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.
This is a typical example of the implementation of a Looper thread,
using the separation of prepare()
and loop()
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(); } }
Modifier and Type | Method and Description |
---|---|
void |
dump(Printer pw,
String prefix)
Dumps the state of the looper for debugging purposes.
|
static Looper |
getMainLooper()
Returns the application's main looper, which lives in the main thread of the application.
|
MessageQueue |
getQueue()
Gets this looper's message queue.
|
Thread |
getThread()
Gets the Thread associated with this Looper.
|
boolean |
isCurrentThread()
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 the
MessageQueue object associated with the current
thread. |
static void |
prepare()
Initialize the current thread as a looper.
|
static void |
prepareMainLooper()
Initialize the current thread as a looper, marking it as an
application's main looper.
|
void |
quit()
Quits the looper.
|
void |
quitSafely()
Quits the looper safely.
|
void |
setMessageLogging(Printer printer)
Control logging of messages as they are processed by this Looper.
|
void |
setTraceTag(long traceTag) |
String |
toString()
Returns a string representation of the object.
|
public static void prepare()
public static void prepareMainLooper()
prepare()
public static Looper getMainLooper()
public static void loop()
quit()
to end the loop.public static Looper myLooper()
public static MessageQueue myQueue()
MessageQueue
object associated with the current
thread. This must be called from a thread running a Looper, or a
NullPointerException will be thrown.public boolean isCurrentThread()
public void setMessageLogging(Printer printer)
printer
- A Printer object that will receive log messages, or
null to disable message logging.public void setTraceTag(long traceTag)
public void quit()
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.
quitSafely()
public void quitSafely()
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.
public Thread getThread()
public MessageQueue getQueue()
public void dump(Printer pw, String prefix)
pw
- A printer to receive the contents of the dump.prefix
- A prefix to prepend to each line which is printed.public String toString()
Object
toString
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())