Android
android.os
public class

android.os.Looper

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)
  synchronized  final  static    Looper  getMainLooper()
Returns the application's main looper, which lives in the main thread of the application.
    final  static    void  loop()
Run the message queue in this thread.
    final  static    Looper  myLooper()
Return the Looper object associated with the current thread.
    final  static    MessageQueue  myQueue()
Return the MessageQueue object associated with the current thread.
    final  static    void  prepare()
Initialize the current thread as a looper.
    final  static    void  prepareMainLooper()
Initialize the current thread as a looper, marking it as an application's main looper.
          void  quit()
          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 the receiver.
Methods inherited from class java.lang.Object

Details

Public Methods

public void dump(Printer pw, String prefix)

public static final synchronized Looper getMainLooper()

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

public static final void loop()

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

public static final Looper myLooper()

Return the Looper object associated with the current thread.

public static final MessageQueue myQueue()

Return the MessageQueue object associated with the current thread.

public static final 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().

public static final 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. prepare()

public void quit()

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.

public String toString()

Returns a string containing a concise, human-readable description of the receiver.

Returns

  • String a printable representation for the receiver.
Copyright 2007 Google Inc. Build 0.9_r1-98467 - 14 Aug 2008 18:56