Thread就是一個工人,負責做工頭交待下來Runnable的事情,只要把事情做完錢領一領就可以走了,有時候可能得等夥伴做完才能繼續做下一件事情,之後跟夥伴一起離開去小酌。
HandlerThread的觀念可以參考多執行緒-handler和thread的關係這篇文章。
Activity程式碼如下:
package com.example.hear;
import java.util.Date;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
public class HeActivity extends Activity {
Handler handler;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_he);
init();
}
private void init() {
handler = new Handler();
handler.postDelayed(runnable, 1000);
}
Runnable runnable = new Runnable() {
public void run() {
Log.i("Now", new Date().toString());
handler.postDelayed(runnable, 1000);
}
};
}
參考文章:
http://j796160836.pixnet.net/blog/post/28766165
http://www.iteye.com/topic/435147
http://android.yaohuiji.com/archives/770
http://developer.android.com/reference/android/os/HandlerThread.html