Most visited

Recently visited

Added in API level 1

Looper

public final class Looper
extends Object

java.lang.Object
   ↳ android.os.Looper


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, call 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();
      }
  }

Summary

Public methods

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.

String toString()

Returns a string representation of the object.

Inherited methods

From class java.lang.Object

Public methods

dump

Added in API level 1
void dump (Printer pw, 
                String prefix)

Dumps the state of the looper for debugging purposes.

Parameters
pw Printer: A printer to receive the contents of the dump.
prefix String: A prefix to prepend to each line which is printed.

getMainLooper

Added in API level 1
Looper getMainLooper ()

Returns the application's main looper, which lives in the main thread of the application.

Returns
Looper

getQueue

Added in API level 23
MessageQueue getQueue ()

Gets this looper's message queue.

Returns
MessageQueue The looper's message queue.

getThread

Added in API level 3
Thread getThread ()

Gets the Thread associated with this Looper.

Returns
Thread The looper's thread.

isCurrentThread

Added in API level 23
boolean isCurrentThread ()

Returns true if the current thread is this looper's thread.

Returns
boolean

loop

Added in API level 1
void loop ()

Run the message queue in this thread. Be sure to call quit() to end the loop.

myLooper

Added in API level 1
Looper myLooper ()

Return the Looper object associated with the current thread. Returns null if the calling thread is not associated with a Looper.

Returns
Looper

myQueue

Added in API level 1
MessageQueue myQueue ()

Return the MessageQueue object associated with the current thread. This must be called from a thread running a Looper, or a NullPointerException will be thrown.

Returns
MessageQueue

prepare

Added in API level 1
void 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 calling quit().

prepareMainLooper

Added in API level 1
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()

quit

Added in API level 1
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 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

Added in API level 18
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 sendMessage(Message) method will return false.

setMessageLogging

Added in API level 1
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 Printer: A Printer object that will receive log messages, or null to disable message logging.

toString

Added in API level 1
String toString ()

Returns a string representation of the object. In general, the 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())
 

Returns
String a string representation of the object.

Hooray!