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

来源:互联网 发布:电视播放软件破解版 编辑:程序博客网 时间:2024/04/30 20:59

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


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

Looper.javapublic 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.javapublic 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);    }   ...}


0 0
原创粉丝点击