public 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) |
static Looper |
getMainLooper()
Returns the application's main looper, which lives in the main thread of the application.
|
MessageQueue |
getQueue() |
Thread |
getThread()
Return the Thread associated with this Looper.
|
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. |
int |
postSyncBarrier()
Posts a synchronization barrier to the Looper's message queue.
|
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 |
removeSyncBarrier(int token)
Removes a synchronization barrier.
|
void |
setMessageLogging(Printer printer)
Control logging of messages as they are processed by this Looper.
|
String |
toString()
Returns a string containing a concise, human-readable description of this
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 void setMessageLogging(Printer printer)
printer
- A Printer object that will receive log messages, or
null to disable message logging.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 void quit()
loop()
method to terminate as soon as possible.public final int postSyncBarrier()
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!removeSyncBarrier(int)
to release the barrier.public final void removeSyncBarrier(int token)
token
- The synchronization barrier token that was returned by
postSyncBarrier()
.IllegalStateException
- if the barrier was not found.public Thread getThread()
public MessageQueue getQueue()
public String toString()
Object
getClass().getName() + '@' + Integer.toHexString(hashCode())
See Writing a useful
toString
method
if you intend implementing your own toString
method.