? ? sMainThreadHandler = new Handler();
? ? ? ? }
? ? ? ? ActivityThread thread = new ActivityThread();
? ? ? ? thread.attach(false);//应用所有的逻辑都在这个方法中
? ? ? ? Looper.loop();//开启一个消息循环,不断的读取MessageQueue中的Message。
? ? }
Looper:
Looper.prepareMainLooper()的代码如下:
/**
? ? * 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.? See also: {@link #prepare()}
? ? */
? ? public static void prepareMainLooper() {
? ? ? ? prepare();
? ? ? ? setMainLooper(myLooper());
? ? ? ? myLooper().mQueue.mQuitAllowed = false;
? ? }
上面的方法注释已经说的很明白,创建了主线程的Looper,这段代码是系统调用的。先看prepare方法做了什么操作。
/** 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()}.
? ? ? */
? ? public static void prepare() {
? ? ? ? if (sThreadLocal.get() != null) {
? ? ? ? ? ? throw new RuntimeException("Only one Looper may be created per thread");
? ? ? ? }
? ? ? ? sThreadLocal.set(new Looper());
? ? }
?
? ? private Looper() {
? ? ? ? mQueue = new MessageQueue();
? ? ? ? mRun = true;
? ? ? ? mThread = Thread.currentThread();//获取当前线程
? ? }
创建主线程的Looper,每一个Looper对应一个Thread、一个MessageQueue,创建Looper的时候会创建一个MessageQueue。到目前位置创建了应用的主线程(Thread)、Looper、MessageQueue,调用Looper.loop(),开始不断的从MessageQueue中读取Message并处理,如果没有消息则等待。现在有了消息循环,有了管理消息循环的Looper就差发送消息和处理消息的Handler了。
Handler:
这个时候你在你的应用中创建一个Handler,一般都是下面的代码:
?private static final Handler mHandler = new Handler() {
? ? ? ? public void handleMessage(Message msg) {
? ? ? ? ? ? ..........
? ? ? ? ? ? }
? ? ? ? }
? ? };
这个Handler是在主线程中创建的,Handler的构造函数如下:
?/**
? ? * Default constructor associates this handler with the queue for the
? ? * current thread.
? ? *
? ? * If there isn't one, this handler won't be able to receive messages.
? ? */
? ? public Handler() {
? ? ? ? mLooper = Looper.myLooper();//获取上面在主线程创建的Looper
? ? ? ? if (mLooper == null) {
? ? ? ? ? ? throw new RuntimeException(
? ? ? ? ? ? ? ? "Can't create handler inside thread that has not called Looper.prepare()");
? ? ? ? }
? ? ? ? mQueue = mLooper.mQueue;//获取Looper的MessageQueue
? ? ? ? mCallback = null;//默认为null在后面处理msg时会就行检查
? ? }
创建完Handler你就可以用了,比如你发一个消息:
Java
mHandler.sendEmptyMessage(MSG_WHAT);
在系统中会走最终走到Handler.java下面的方法:
/**
? ? * Enqueue a message into the message queue after all pending messages
? ? * before the absolute time (in milliseconds) uptimeMillis.
? ? * The time-base is {@link android.os.SystemClock#uptimeMillis}.
? ? * You will receive it in {@link #handleMessage}, in the thread attached
? ? * to this handler.
? ? *
? ? * @param uptimeMillis The absolute time at which the message should be
? ? *? ? ? ? delivered, using the
? ? *? ? ? ? {@link android.os.SystemClock#uptimeMillis} time-base.
? ? *? ? ? ?
? ? * @return Returns true if the message was successfully placed in to the
? ? *? ? ? ? message queue.? Returns false on failure, usually because the
? ? *? ? ? ? looper processing the message queue is exiting.? Note that a
? ? *? ? ? ? result of true does not mean the message will be processed -- if
? ? *? ? ? ? the looper is quit before the delivery time of the message
? ? *? ? ? ? occurs then the message will be dropped.
? ? */
? ? public boolean sendMessageAtTime(Message msg, long uptimeMillis)
? ? {
? ? ? ? boolean sent = false;
? ? ? ? MessageQueue queue = mQueue;
? ? ? ? if (queue != null) {
? ? ? ? ? ? msg.targe