相較於實作基本的訊息傳送機制(Handler + Looper + Message) Android 有另一個已經幫我們完成 Looper 連結的 Thread,名稱為 Handler Thread
Handler Thread 其實相當簡單,source code 也僅150行,如下

/*
 * Copyright (C) 2006 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package android.os;
/**
 * Handy class for starting a new thread that has a looper. The looper can then be
 * used to create handler classes. Note that start() must still be called.
 */
public class HandlerThread extends Thread {
    int mPriority;
    int mTid = -1;
    Looper mLooper;
    public HandlerThread(String name) {
        super(name);
        mPriority = Process.THREAD_PRIORITY_DEFAULT;
    }
    /**
     * Constructs a HandlerThread.
     * @param name
     * @param priority The priority to run the thread at. The value supplied must be from
     * {@link android.os.Process} and not from java.lang.Thread.
     */
    public HandlerThread(String name, int priority) {
        super(name);
        mPriority = priority;
    }
    /**
     * Call back method that can be explicitly overridden if needed to execute some
     * setup before Looper loops.
     */
    protected void onLooperPrepared() {
    }
    @Override
    public void run() {
        mTid = Process.myTid();
        Looper.prepare();
        synchronized (this) {
            mLooper = Looper.myLooper();
            notifyAll();
        }
        Process.setThreadPriority(mPriority);
        onLooperPrepared();
        Looper.loop();
        mTid = -1;
    }
    /**
     * This method returns the Looper associated with this thread. If this thread not been started
     * or for any reason is isAlive() returns false, this method will return null. If this thread
     * has been started, this method will block until the looper has been initialized.
     * @return The looper.
     */
    public Looper getLooper() {
        if (!isAlive()) {
            return null;
        }
        // If the thread has been started, wait until the looper has been created.
        synchronized (this) {
            while (isAlive() && mLooper == null) {
                try {
                    wait();
                } catch (InterruptedException e) {
                }
            }
        }
        return mLooper;
    }
    /**
     * Quits the handler thread's looper.
     * <p>
     * Causes the handler thread's looper to terminate without processing any
     * more messages in the message queue.
     * </p><p>
     * Any attempt to post messages to the queue after the looper is asked to quit will fail.
     * For example, the {@link Handler#sendMessage(Message)} method will return false.
     * </p><p class="note">
     * Using this method may be unsafe because some messages may not be delivered
     * before the looper terminates.  Consider using {@link #quitSafely} instead to ensure
     * that all pending work is completed in an orderly manner.
     * </p>
     *
     * @return True if the looper looper has been asked to quit or false if the
     * thread had not yet started running.
     *
     * @see #quitSafely
     */
    public boolean quit() {
        Looper looper = getLooper();
        if (looper != null) {
            looper.quit();
            return true;
        }
        return false;
    }
    /**
     * Quits the handler thread's looper safely.
     * <p>
     * Causes the handler thread's looper to terminate as soon as all remaining messages
     * in the message queue that are already due to be delivered have been handled.
     * Pending delayed messages with due times in the future will not be delivered.
     * </p><p>
     * Any attempt to post messages to the queue after the looper is asked to quit will fail.
     * For example, the {@link Handler#sendMessage(Message)} method will return false.
     * </p><p>
     * If the thread has not been started or has finished (that is if
     * {@link #getLooper} returns null), then false is returned.
     * Otherwise the looper is asked to quit and true is returned.
     * </p>
     *
     * @return True if the looper looper has been asked to quit or false if the
     * thread had not yet started running.
     */
    public boolean quitSafely() {
        Looper looper = getLooper();
        if (looper != null) {
            looper.quitSafely();
            return true;
        }
        return false;
    }
    /**
     * Returns the identifier of this thread. See Process.myTid().
     */
    public int getThreadId() {
        return mTid;
    }
}

 
在 run 方法已經幫我們做好了 Looper.prepare 以及 Looper.loop,我們只需要實作 HandlerThread 的 subclass,並複寫 onLooperPrepared() 方法,在該方法中定義初始化的工作,建構式可以輸入該 thread 名稱,方便之後除錯。
預設的 priority 為 Process.THREAD_PROCESS_DEFAULT
其他需要注意的部份為
1.與外部 Handler 的連結必須透過 getLooper 方法來完成。
2.啟動 HandlerThread 的方法還是一樣呼叫 start()。
3.可以透過呼叫 quit 以及 quitSafely 來中止 HandlerThread。
呼叫 quit 會立刻中止 looper,如果MessageQueue(MQ) 中還有訊息,這些訊息不會被處理。

4.呼叫 quitSafely 不會立刻中止 looper,looper 會處理完 Message Queue 中可以發送的訊息再停止。
如下

public class CustomizeHandlerThread extends HandlerThread
{
    private CustomizeHandler mHandler;
    public CustomizeHandlerThread(String name, int priority) {
        super(name, priority);
    }
    @Override
    protected void onLooperPrepared()
    {
        super.onLooperPrepared();
        mHandler = new CustomizeHandler(getLooper());
    }
    public Handler getHandler()
    {
        return mHandler;
    }
}

 
CustomizeHandler 則是自定義的 Handler subclass,不使用匿名內部類別的 Handler

class CustomizeHandler extends Handler
{
    private static final String TAG = "CustomizeHandler";
    public CustomizeHandler(Looper looper) {
        super(looper);
    }
    @Override
    public void handleMessage(Message msg)
    {
        super.handleMessage(msg);
        switch (msg.what) {
            case 0:
                Log.d(TAG, "in handle message what 0");
                break;
            case 1:
                Log.d(TAG, "in handle message what 1");
                break;
        }
    }
}

 
MainActivity 使用範例

package com.foxx.handlerthread;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Process;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import com.example.handlerthread.R;
public class MainActivity extends ActionBarActivity implements OnClickListener
{
    private static final String TAG = "MainActivity";
    private Button mStartThreadButton;
    private TextView mResultTextView;
    private CustomizeHandlerThread mCustomizeWorkerThread;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initUIComponents();
        startWorkerThread();
    }
    private void initUIComponents()
    {
        mStartThreadButton = (Button) findViewById(R.id.startThread);
        mStartThreadButton.setOnClickListener(this);
        mResultTextView = (TextView) findViewById(R.id.textview);
    }
    private void startWorkerThread()
    {
        mCustomizeWorkerThread = new CustomizeHandlerThread("CustomizeHandlerThread",
                Process.THREAD_PRIORITY_BACKGROUND);
        mCustomizeWorkerThread.start();
    }
    @Override
    public void onClick(View view)
    {
        int uiId = view.getId();
        switch (uiId) {
            case R.id.startThread:
                sendMessageToWorkerHandler();
                break;
        }
    }
    private void sendMessageToWorkerHandler()
    {
        Handler handler = mCustomizeWorkerThread.getHandler();
        Message message = handler.obtainMessage(0);
        handler.sendMessage(message);
    }
    public TextView getResultTextView()
    {
        return mResultTextView;
    }
    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        mCustomizeWorkerThread.getHandler().getLooper().quit();
    }
}

 
若想將結果回傳給 MainActivity,可以考慮新增建構子,將 MainActivity 傳入CustomizeHandler.java

class CustomizeHandler extends Handler
{
    ...
    private MainActivity mMainActivity;
    public CustomizeHandler(Looper looper, MainActivity activity) {
        super(looper);
        mMainActivity = activity;
    }
    @Override
    public void handleMessage(Message msg)
    {
        super.handleMessage(msg);
        switch (msg.what) {
            case 0:
                setResultForMainActivity();
                break;
            case 1:
                Log.d(TAG, "in handle message what 1");
                break;
        }
    }
    private void setResultForMainActivity()
    {
        mMainActivity.runOnUiThread(new Runnable() {
            @Override
            public void run()
            {
                mMainActivity.getResultTextView().setText("0");
            }
        });
    }
}

 
修改相對應的 CustomizeHandlerThread

public class CustomizeHandlerThread extends HandlerThread
{
    ...
    private MainActivity mMainActivity;
    public CustomizeHandlerThread(MainActivity activity, String name, int priority){
        super(name, priority);
        mMainActivity = activity;
    }
    ...
}

 
最後修改 MainActivity.java

public class MainActivity extends ActionBarActivity implements OnClickListener
{
    ...
    private void startWorkerThread()
    {
        mCustomizeWorkerThread = new CustomizeHandlerThread(this,"CustomizeHandlerThread",
                Process.THREAD_PRIORITY_BACKGROUND);
        mCustomizeWorkerThread.start();
    }
    ...
}

 
HandlerThread 只使用一個 MessageQueue,所以是執行緒安全的,適合用於循序動作。
多個任務互相依賴結合的情況也適合用於 HandlerThread,可藉由 Message.what 區分不同任務,並判斷是否需要進行下一個任務。
基本上如果任務不需要並發執行,也不需要對 Looper 作太多的控制,使用 Handler Thread會比完全實作訊息傳送機制來的簡單和方便。
 
Android AsyncTask
Android Worker Thread(Handler + Looper + Message)