首页 > 代码库 > Android 消息处理源代码分析(2)

Android 消息处理源代码分析(2)

 Android 消息处理源代码分析(1)点击打开链接


继续接着分析剩下的类文件

Looper.java
public final class Looper {
    final MessageQueue mQueue;   //消息队列
    final Thread mThread;   //Looper联系的线程
    
    public static void prepare() {
        prepare(true);
    }
    
    private static void prepare(boolean quitAllowed) {   //先会检查是否有Looper。若有则抛出异常。没有的话则创建一个Looper实例保存起来
        if (sThreadLocal.get() != null) {
            throw new RuntimeException("Only one Looper may be created per thread");
        }
        sThreadLocal.set(new Looper(quitAllowed));
    }

    public static void prepareMainLooper() {
        prepare(false);
        synchronized (Looper.class) {
            if (sMainLooper != null) {
                throw new IllegalStateException("The main Looper has already been prepared.");
            }
            sMainLooper = myLooper();
        }
    }
   //在这个线程中执行消息队列。调用quit()停止
   public static void loop() {
	...
	final MessageQueue queue = me.mQueue;

        // Make sure the identity of this thread is that of the local process,
        // and keep track of what that identity token actually is.
        Binder.clearCallingIdentity();
        final long ident = Binder.clearCallingIdentity();

        for (;;) {
            Message msg = queue.next(); // 从消息队列中取出一条消息
            if (msg == null) {
                // No message indicates that the message queue is quitting.
                return;
            }

            // This must be in a local variable, in case a UI event sets the logger
            Printer logging = me.mLogging;
            if (logging != null) {
                logging.println(">>>>> Dispatching to " + msg.target + " " +
                        msg.callback + ": " + msg.what);
            }

            msg.target.dispatchMessage(msg);   //交给msg的handler分发消息处理

	...
   }
    //取出当前线程的Looper,返回空则表示当前线程没有Looper
    public static Looper myLooper() {
        return sThreadLocal.get();
    }
}








Handler.java
public class Handler {
     //定义Callback接口
     public interface Callback {
   	   public boolean handleMessage(Message msg);
     }
	
     //子类要实现的消息处理方法
     public void handleMessage(Message msg) {
     }

     * Handle system messages here.
     */
    public void dispatchMessage(Message msg) {        //分发信息
        if (msg.callback != null) {          //若指定了msg.callback,则由它处理
            handleCallback(msg);
        } else {
            if (mCallback != null) {        //若指定了Handler.mCallback。则由它处理
                if (mCallback.handleMessage(msg)) {    //调用mCallback接口的实现方法
                    return;
                }
            }
            handleMessage(msg);   最后才调用Handler自身重载的handleMessage方法
        }
    }
    分发消息函数中,消息先会检查自身有没有处理自身的回调Runnable,若有则由它处理。若没有则会检查该handler有无自身的回调处理,若有则调用,若没有则调用自身重载的handleMessage方法

    //Handler的生成总是和它当前所处线程有关的,假设当前线程中没有一个Looper。则会报错。UI线程中默认有产生Looper的函数
    public Handler() {
        this(null, false);
    }    

    //使用指定的Looper(能够处理那个Looper线程中的消息)。不用默认的从当前线程中取出Looper
    public Handler(Looper looper) {
        this(looper, null, false);
    }
   ...
}


Android 消息处理源代码分析(2)