Handler/Looper/MessageQueue

2017-10-10  本文已影响0人  姜小鱼Qyer
final MessageQueue mQueue;
private Looper(boolean quitAllowed){
    mQueue = new MessageQueue(quitAllowed);
    mRun = true;
    mThread = Thread.currentThread();// Looper与当前线程建立对应关系
}

HandlerLooperMessageQueue建立联系

public Handler(){
/*省略部分代码*/
    mLooper = Looper.myLooper();// 通过sThreadLocal.get()来获取当前线程中的Looper实例
    mQueue = mLooper.mQueue;// mQueue是Looper与Handler之间沟通的桥梁
    mCallback = callback;
}
/**
     * Return the Looper object associated with the current thread.  Returns
     * null if the calling thread is not associated with a Looper.
     */
    public static @Nullable Looper myLooper() {
        return sThreadLocal.get();
    }
上一篇下一篇

猜你喜欢

热点阅读