Handler()源码:
myLooper()源码:mLooper = Looper.myLooper(); if (mLooper == null) { throw new RuntimeException( "Can't create handler inside thread that has not called Looper.prepare()"); } mQueue = mLooper.mQueue;
在UI线程中,Handler会拿到与UI线程(UI线程创建的时候会创建Looper)关联的Looper对象。public static @Nullable Looper myLooper() { return sThreadLocal.get(); }
然后在拿到mQueue,即MessageQueue对象。sendMessage():将获得的Message对象放入一个MessageQueue队列中轮循发送。Looper中的loop()方法实际通过dispatchMessage()将队列中的Message进行获取,最后回调handleMessage()。