標籤:
Android中的Looper類,是用來封裝訊息迴圈和訊息佇列的一個類,用於在android線程中進行訊息處理。handler事實上能夠看做是一個工具類,用來向訊息佇列中插入訊息的。
(1) Looper類用來為一個線程開啟一個訊息迴圈。 預設情況下android中新誕生的線程是沒有開啟訊息迴圈的。(主線程除外,主線程系統會自己主動為其建立Looper對象,開啟訊息迴圈。) Looper對象通過MessageQueue來存放訊息和事件。一個線程僅僅能有一個Looper,相應一個MessageQueue。
(2) 一般是通過Handler對象來與Looper進行互動的。Handler可看做是Looper的一個介面,用來向指定的Looper發送訊息及定義處理方法。 預設情況下Handler會與其被定義時所線上程的Looper綁定,比方,Handler在主線程中定義,那麼它是與主線程的Looper綁定。 mainHandler = new Handler() 等價於new Handler(Looper.myLooper()). Looper.myLooper():擷取當前進程的looper對象,類似的 Looper.getMainLooper() 用於擷取主線程的Looper對象。
(3) 在非主線程中直接new Handler() 會報例如以下的錯誤: E/AndroidRuntime( 6173): Uncaught handler: thread Thread-8 exiting due to uncaught exception E/AndroidRuntime( 6173): java.lang.RuntimeException: Can‘t create handler inside thread that has not called Looper.prepare() 原因是非主線程中預設沒有建立Looper對象,須要先調用Looper.prepare()啟用Looper。
(4) Looper.loop(); 讓Looper開始工作,從訊息佇列裡取訊息,處理訊息。
注意:寫在Looper.loop()之後的代碼不會被執行,這個函數內部應該是一個迴圈,當調用mHandler.getLooper().quit()後,loop才會中止,其後的代碼才幹得以執行。
(5) 基於以上知識,可實現主線程給子線程(非主線程)發送訊息。
把以下範例中的mHandler聲明成類成員,在主線程通過mHandler發送訊息就可以。 Android官方文檔中Looper的介紹: Class used to run a message loop for a thread. Threads by default do not have a message loop associated with them; to create one, call prepare() in the thread that is to run the loop, and then loop() to have it process messages until the loop is stopped.
Most interaction with a message loop is through the Handler class.
This is a typical example of the implementation of a Looper thread, using the separation of prepare() and loop() to create an initial Handler to communicate with the Looper.
class LooperThread extends Thread{public Handler mHandler;public void run() {Looper.prepare();mHandler = new Handler() {public void handleMessage(Message msg) {// process incoming messages here}};Looper.loop();}
假設線程中使用Looper.prepare()和Looper.loop()建立了訊息佇列就能夠讓訊息處理在該線程中完畢。
android HandlerThread使用小例
之前研究過handler 和 looper 訊息佇列,只是android裡的handler不是另外開啟線程來啟動並執行,還是在主UI線程中,假設想另啟線程的話須要用到HandlerThread來實現。在使用HandlerThread的時候須要實現CallBack介面以重寫handlerMessage方法,在handlerMessage方法中來處理自己的邏輯。下來給出一個小範例程式。
layout檔案非常easy,就一個button來啟動HanlderTread線程
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/handlerThreadBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="startHandlerThread" /></LinearLayout>
Activity代碼例如以下:
package com.tayue;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Handler.Callback;import android.os.HandlerThread;import android.os.Message;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;/** * * @author xionglei * */public class TestHandlerActivity extends Activity implements OnClickListener{ public Button handlerThreadBTN; MyHandlerThread handlerThread; Handler handler; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //列印UI線程的名稱 System.out.println("onCreate CurrentThread = " + Thread.currentThread().getName()); setContentView(R.layout.main); handlerThreadBTN = (Button) findViewById(R.id.handlerThreadBtn); handlerThreadBTN.setOnClickListener(this); handlerThread = new MyHandlerThread("myHanler"); handlerThread.start(); //注意: 這裡必須用到handler的這個構造器,由於須要把callback傳進去,從而使自己的HandlerThread的handlerMessage來替換掉Handler原生的handlerThread handler = new Handler(handlerThread.getLooper(), handlerThread); } @Override public void onClick(View v) { //點擊button後來開啟線程 handler.sendEmptyMessage(1); } private class MyHandlerThread extends HandlerThread implements Callback { public MyHandlerThread(String name) { super(name); } @Override public boolean handleMessage(Message msg) { //列印線程的名稱 System.out.println(" handleMessage CurrentThread = " + Thread.currentThread().getName()); return true; } } }
點擊button,列印的日誌例如以下(這裡點擊了3次) 07-06 09:32:48.776: I/System.out(780): onCreate CurrentThread = main 07-06 09:32:55.076: I/System.out(780): handleMessage CurrentThread = myHanler 07-06 09:32:58.669: I/System.out(780): handleMessage CurrentThread = myHanler 07-06 09:33:03.476: I/System.out(780): handleMessage CurrentThread = myHanler
HandlerThread就這麼簡單。
當然 android自己也有非同步線程的handler,就是AsyncTask,這個類就是封裝了HandlerThread 和handler來實現非同步多線程的操作的。
相同能夠這樣使用:
private boolean iscancel = false; //使用者手動取消登入的標誌位 handlerThread = new HandlerThread("myHandlerThread"); handlerThread.start(); handler = new MyHandler(handlerThread.getLooper()); // 將要啟動並執行線程對象加入到線程隊列中 handler.post(new Runnable() { @Override public void run() { Message message = handler.obtainMessage(); UserBean user = Bbs.getInstance().Login(username, password);//耗時任務 Bundle b = new Bundle(); b.putSerializable("user", user); message.setData(b); message.sendToTarget(); //或使用 handler.sendMessage(message); } }); class MyHandler extends Handler { public MyHandler(Looper looper) { super(looper); } @Override public void handleMessage(Message msg) { if(iscancel == false){ // 操作UI線程的代碼 Bundle b = msg.getData(); UserBean user = (UserBean)b.get("user"); ...... } } }
Android -- Looper.prepare()和Looper.loop() —深入版