FileDocCategorySizeDatePackage
Looper.javaAPI DocAndroid 1.5 API7451Wed May 06 22:41:56 BST 2009android.os

Looper

public class Looper extends Object
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 {@link #prepare} in the thread that is to run the loop, and then {@link #loop} to have it process messages until the loop is stopped.

Most interaction with a message loop is through the {@link Handler} class.

This is a typical example of the implementation of a Looper thread, using the separation of {@link #prepare} and {@link #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();
}
}

Fields Summary
private static final boolean
DEBUG
private static final boolean
localLOGV
private static final ThreadLocal
sThreadLocal
final MessageQueue
mQueue
volatile boolean
mRun
Thread
mThread
private android.util.Printer
mLogging
private static Looper
mMainLooper
Constructors Summary
private Looper()

        mQueue = new MessageQueue();
        mRun = true;
        mThread = Thread.currentThread();
    
Methods Summary
public voiddump(android.util.Printer pw, java.lang.String prefix)

        pw.println(prefix + this);
        pw.println(prefix + "mRun=" + mRun);
        pw.println(prefix + "mThread=" + mThread);
        pw.println(prefix + "mQueue=" + ((mQueue != null) ? mQueue : "(null"));
        if (mQueue != null) {
            synchronized (mQueue) {
                Message msg = mQueue.mMessages;
                int n = 0;
                while (msg != null) {
                    pw.println(prefix + "  Message " + n + ": " + msg);
                    n++;
                    msg = msg.next;
                }
                pw.println(prefix + "(Total messages: " + n + ")");
            }
        }
    
public static final synchronized android.os.LoopergetMainLooper()
Returns the application's main looper, which lives in the main thread of the application.

        return mMainLooper;
    
public java.lang.ThreadgetThread()
Return the Thread associated with this Looper.

        return mThread;
    
public static final voidloop()
Run the message queue in this thread. Be sure to call {@link #quit()} to end the loop.

        Looper me = myLooper();
        MessageQueue queue = me.mQueue;
        while (true) {
            Message msg = queue.next(); // might block
            //if (!me.mRun) {
            //    break;
            //}
            if (msg != null) {
                if (msg.target == null) {
                    // No target is a magic identifier for the quit message.
                    return;
                }
                if (me.mLogging!= null) me.mLogging.println(
                        ">>>>> Dispatching to " + msg.target + " "
                        + msg.callback + ": " + msg.what
                        );
                msg.target.dispatchMessage(msg);
                if (me.mLogging!= null) me.mLogging.println(
                        "<<<<< Finished to    " + msg.target + " "
                        + msg.callback);
                msg.recycle();
            }
        }
    
public static final android.os.LoopermyLooper()
Return the Looper object associated with the current thread. Returns null if the calling thread is not associated with a Looper.

        return (Looper)sThreadLocal.get();
    
public static final MessageQueuemyQueue()
Return the {@link MessageQueue} object associated with the current thread. This must be called from a thread running a Looper, or a NullPointerException will be thrown.

        return myLooper().mQueue;
    
public static final voidprepare()
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 {@link #loop()} after calling this method, and end it by calling {@link #quit()}.

    
                                                     
         
        if (sThreadLocal.get() != null) {
            throw new RuntimeException("Only one Looper may be created per thread");
        }
        sThreadLocal.set(new Looper());
    
public static final voidprepareMainLooper()
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. {@link #prepare()}

        prepare();
        setMainLooper(myLooper());
        if (Process.supportsProcesses()) {
            myLooper().mQueue.mQuitAllowed = false;
        }
    
public voidquit()

        Message msg = Message.obtain();
        // NOTE: By enqueueing directly into the message queue, the
        // message is left with a null target.  This is how we know it is
        // a quit message.
        mQueue.enqueueMessage(msg, 0);
    
private static synchronized voidsetMainLooper(android.os.Looper looper)

        mMainLooper = looper;
    
public voidsetMessageLogging(android.util.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.

param
printer A Printer object that will receive log messages, or null to disable message logging.

        mLogging = printer;
    
public java.lang.StringtoString()

        return "Looper{"
            + Integer.toHexString(System.identityHashCode(this))
            + "}";