TestLooperManager  |  API reference  |  Android Developers


public class TestLooperManager
extends Object



Blocks a looper from executing any messages, and allows the holder of this object to control when and which messages get executed until it is released.

A TestLooperManager should be acquired using Instrumentation.acquireLooperManager(Looper). Until release() is called, the Looper thread will not execute any messages except when execute(Message) is called. The test code may use next() to acquire messages that have been queued to this Looper/MessageQueue and then execute(Message) to run any that desires.

Summary

Public methods

void execute(Message message)

Executes the given message on the Looper thread this wrapper is attached to.

MessageQueue getMessageQueue()

Returns the MessageQueue this object is wrapping.

boolean hasMessages(Handler h, Object object, Runnable r)

Returns true if there are any queued messages that match the parameters.

boolean hasMessages(Handler h, Object object, int what)

Returns true if there are any queued messages that match the parameters.

boolean isBlockedOnSyncBarrier()

Checks whether the Looper is currently blocked on a sync barrier.

Message next()

Returns the next message that should be executed by this queue, may block if no messages are ready.

Long peekWhen()

Retrieves, but does not remove, the values of ERROR(Message.when/android.os.Message#when Message.when) of next message in the queue.

Message poll()

Retrieves and removes the next message in this queue.

void recycle(Message msg)

Called to indicate that a Message returned by next() has been parsed and should be recycled.

void release()

Releases the looper to continue standard looping and processing of messages, no further interactions with TestLooperManager will be allowed after release() has been called.

Inherited methods

From class java.lang.Object

Object clone()

Creates and returns a copy of this object.

boolean equals(Object obj)

Indicates whether some other object is "equal to" this one.

void finalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

final Class<?> getClass()

Returns the runtime class of this Object.

int hashCode()

Returns a hash code value for the object.

final void notify()

Wakes up a single thread that is waiting on this object's monitor.

final void notifyAll()

Wakes up all threads that are waiting on this object's monitor.

String toString()

Returns a string representation of the object.

final void wait(long timeoutMillis, int nanos)

Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

final void wait(long timeoutMillis)

Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

final void wait()

Causes the current thread to wait until it is awakened, typically by being notified or interrupted.

Public methods

execute

public void execute (Message message)

Executes the given message on the Looper thread this wrapper is attached to.

Execution will happen on the Looper's thread (whether it is the current thread or not), but all RuntimeExceptions encountered while executing the message will be thrown on the calling thread.

Parameters
message Message

hasMessages

public boolean hasMessages (Handler h, 
                Object object, 
                int what)

Returns true if there are any queued messages that match the parameters.

Parameters
h Handler: the value of Message.getTarget()
object Object: the value of Message.obj, null for any
what int: the value of Message.what
Returns
boolean

isBlockedOnSyncBarrier

public boolean isBlockedOnSyncBarrier ()

Checks whether the Looper is currently blocked on a sync barrier.

Returns
boolean

next

public Message next ()

Returns the next message that should be executed by this queue, may block if no messages are ready.

Callers should always call recycle(Message) on the message when all interactions with it have completed.

Returns
Message

poll

public Message poll ()

Retrieves and removes the next message in this queue. If the queue is empty, returns null. This method never blocks.

Callers should always call recycle(Message) on the message when all interactions with it have completed.

Returns
Message

recycle

public void recycle (Message msg)

Called to indicate that a Message returned by next() has been parsed and should be recycled.

Parameters
msg Message

release

public void release ()

Releases the looper to continue standard looping and processing of messages, no further interactions with TestLooperManager will be allowed after release() has been called.