Android Handler Looper Message MessageQueue Thread
Handler负责发送Message和处理Message.
Handler处理Message必须对应一个Looper,Looper负责从MessageQueue中抽取Message.
如果Handler定义在main thread( ui thread)中,那么默认情况下系统会给其关联一个Looper。所以ui thread中定义的Handler可以更新UI.
如果Handler定义在非主线程中,必须要有一个Looper与之关联,才能处理Message。一般
class MyThread extends Thread() {
private Handler mHandler;
public void run() {
Looper.prepare();
mHandler = new Handler() {
public void handleMessage(Message msg) {
// process incoming messages here
}
}
Looper.loop();
}
}
使用Handler的时候要注意,Handler在哪个线程中定义,不正确的线程中直接更新UI将引发异常。
几种从其他线程访问UI线程:
- Activity.runOnUiThread(Runnable);
- View.post(Runnable)
- View.postDelayed(Runnable, long)
- Handler实现线程间的通信
- AsyncTask
package com.leamiko.activity;
import com.leamiko.orm.R;
import android.app.Activity;
import android.os.AsyncTask;
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;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView txView;
private Button button;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
Log.i("RootyInfo", "oncreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txView = (TextView) findViewById(R.id.textView1);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 创建一个用于展示前三种后台线程和UI线程交互的线程
new TestThread(MainActivity.this).start();
// 创建一个用于展示AsyncTask实现交互的TestAsyncTask
new TestAsyncTask().execute("Test", " AsyncTask");
}
});
}
class TestAsyncTask extends AsyncTask<String, Integer, String> {
// TestAsyncTask被后台线程执行后,被UI线程被调用,一般用于初始化界面控件,如进度条
@Override
protected void onPreExecute() {
super.onPreExecute();
}
// doInBackground执行完后由UI线程调用,用于更新界面操作
@Override
protected void onPostExecute(String result) {
txView.setText(result);
super.onPostExecute(result);
}
// 在PreExcute执行后被启动AysncTask的后台线程调用,将结果返回给UI线程
@Override
protected String doInBackground(String... params) {
StringBuffer sb = new StringBuffer();
for (String string : params) {
sb.append(string);
}
return sb.toString();
}
}
// 用于线程间通信的Handler
class TestHandler extends Handler {
public TestHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) {
txView.setText((String) msg.getData().get("tag"));
super.handleMessage(msg);
}
}
// 后台线程类
class TestThread extends Thread {
Activity activity;
public TestThread(Activity activity) {
this.activity = activity;
}
@Override
public void run() {
// 下面代码用来演示Activity.runOnUIThread(Runnable)方法的实现
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
txView.setText("Test runOnUIThread");
}
});
// 下面代码用来演示Activity.runOnUIThread(Runnable)方法的实现
txView.post(new Runnable() {
@Override
public void run() {
txView.setText("Test View.post(Runnable)");
}
});
// 下面代码用来演示Activity.runOnUIThread(Runnable)方法的实现
txView.postDelayed(new Runnable() {
@Override
public void run() {
txView.setText("Test View.postDelay(Runnable,long)");
}
}, 1000);
// 下面代码用来演示Handler方法的实现
Message msg = new Message();
Bundle bundle = new Bundle();
bundle.putString("tag", "Test Handler");
msg.setData(bundle);
new TestHandler(Looper.getMainLooper()).sendMessage(msg);
}
}
}