概述:Android使用消息機制實現線程間的通信,線程通過Looper建立自己的消息循 環,MessageQueue是FIFO的消息隊列,Looper負責從MessageQueue中取出消息,並且分發到消息指定目標Handler對 象。Handler對象綁定到線程的局部變量Looper,封裝了發送消息和處理消息的接口。
例子:在介紹原理之前,我們先介紹Android線程通訊的一個例子,這個例子實現點擊按鈕之後從主線程發送消息"hello"到另外一個名爲” CustomThread”的線程。
LooperThreadActivity.java
package com.zhuozhuo;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
public class LooperThreadActivity extends Activity{
/** Called when the activity is first created. */
private final int MSG_HELLO = 0;
private Handler mHandler;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new CustomThread().start();//新建並啓動CustomThread實例
findViewById(R.id.send_btn).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {//點擊界面時發送消息
String str = "hello";
Log.d("Test", "MainThread is ready to send msg:" + str);
mHandler.obtainMessage(MSG_HELLO, str).sendToTarget();//發送消息到CustomThread實例
}
});
}
class CustomThread extends Thread {
@Override
public void run() {
//建立消息循環的步驟
Looper.prepare();//1、初始化Looper
mHandler = new Handler(){//2、綁定handler到CustomThread實例的Looper對象
public void handleMessage (Message msg) {//3、定義處理消息的方法
switch(msg.what) {
case MSG_HELLO:
Log.d("Test", "CustomThread receive msg:" + (String) msg.obj);
}
}
};
Looper.loop();//4、啓動消息循環
}
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button android:text="發送消息" android:id="@+id/send_btn" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
Log打印結果:
原理:
我們看到,爲一個線程建立消息循環有四個步驟:
1、 初始化Looper
2、 綁定handler到CustomThread實例的Looper對象
3、 定義處理消息的方法
4、 啓動消息循環
下面我們以這個例子爲線索,深入Android源代碼,說明Android Framework是如何建立消息循環,並對消息進行分發的。
1、 初始化Looper : Looper.prepare()
Looper.java
private static final ThreadLocal sThreadLocal = new ThreadLocal();
public static final void prepare() {
if (sThreadLocal.get() != null) {
throw new RuntimeException("Only one Looper may be created per thread");
}
sThreadLocal.set(new Looper());
}
一個線程在調用Looper的靜態方法prepare()時,這個線程會新建一個Looper對象,並放入到線程的局部變量中,而這個變量是不和其他線程共享的(關於ThreadLocal的介紹)。下面我們看看Looper()這個構造函數:
Looper.java
final MessageQueue mQueue;
private Looper() {
mQueue = new MessageQueue();
mRun = true;
mThread = Thread.currentThread();
}
可以看到在Looper的構造函數中,創建了一個消息隊列對象mQueue,此時,調用Looper. prepare()的線程就建立起一個消息循環的對象(此時還沒開始進行消息循環)。
2、 綁定handler到CustomThread實例的Looper對象 : mHandler= new Handler()
Handler.java
final MessageQueue mQueue;
final Looper mLooper;
public Handler() {
if (FIND_POTENTIAL_LEAKS) {
final Class<? extends Handler> klass = getClass();
if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) &&
(klass.getModifiers() & Modifier.STATIC) == 0) {
Log.w(TAG, "The following Handler class should be static or leaks might occur: " +
klass.getCanonicalName());
}
}
mLooper = Looper.myLooper();
if (mLooper == null) {
throw new RuntimeException(
"Can't create handler inside thread that has not called Looper.prepare()");
}
mQueue = mLooper.mQueue;
mCallback = null;
}
Handler通過mLooper = Looper.myLooper();綁定到線程的局部變量Looper上去,同時Handler通過mQueue =mLooper.mQueue;獲得線程的消息隊列。此時,Handler就綁定到創建此Handler對象的線程的消息隊列上了。
3、定義處理消息的方法:Override public void handleMessage (Message msg){}
子類需要覆蓋這個方法,實現接受到消息後的處理方法。
4、啓動消息循環 : Looper.loop()
所有準備工作都準備好了,是時候啓動消息循環了!Looper的靜態方法loop()實現了消息循環。
Looper.java
public static final void loop() {
Looper me = myLooper();
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();
while (true) {
Message msg = queue.next(); // might block
//if (!me.mRun) {
// break;
//}
if (msg != null) {
if (msg.target == null) {
// No target is a magic identifier for the quit message.
return;
}
if (me.mLogging!= null) me.mLogging.println(
">>>>> Dispatching to " + msg.target + " "
+ msg.callback + ": " + msg.what
);
msg.target.dispatchMessage(msg);
if (me.mLogging!= null) me.mLogging.println(
"<<<<< Finished to " + msg.target + " "
+ msg.callback);
// Make sure that during the course of dispatching the
// identity of the thread wasn't corrupted.
final long newIdent = Binder.clearCallingIdentity();
if (ident != newIdent) {
Log.wtf("Looper", "Thread identity changed from 0x"
+ Long.toHexString(ident) + " to 0x"
+ Long.toHexString(newIdent) + " while dispatching to "
+ msg.target.getClass().getName() + " "
+ msg.callback + " what=" + msg.what);
}
msg.recycle();
}
}
}
while(true)體現了消息循環中的“循環“,Looper會在循環體中調用queue.next()獲取消息隊 列中需要處理的下一條消息。當msg != null且msg.target != null時,調用msg.target.dispatchMessage(msg);分發消息,當分發完成後,調用msg.recycle();回收消 息。
msg.target是一個handler對象,表示需要處理這個消息的handler對象。Handler的void dispatchMessage(Message msg)方法如下:
Handler.java
public void dispatchMessage(Message msg) {
if (msg.callback != null) {
handleCallback(msg);
} else {
if (mCallback != null) {
if (mCallback.handleMessage(msg)) {
return;
}
}
handleMessage(msg);
}
}
可見,當msg.callback== null 並且mCallback == null時,這個例子是由handleMessage(msg);處理消息,上面我們說到子類覆蓋這個方法可以實現消息的具體處理過程。
總結:從上面的分析過程可知,消息循環的核心是Looper,Looper持有消息 隊列MessageQueue對象,一個線程可以把Looper設爲該線程的局部變量,這就相當於這個線程建立了一個對應的消息隊列。Handler的作 用就是封裝發送消息和處理消息的過程,讓其他線程只需要操作Handler就可以發消息給創建Handler的線程。由此可以知道,UI線程在創建的時候就建立了消息循環(在ActivityThread的public static final void main(String[] args)方法中實現),因此我們可以在其他線程給UI線程的handler發送消息,達到更新UI的目的。