在Android,线程分为有消息循环的线程和没有消息循环的线程,有消息循环的线程一般都会有一个Looper。我们的主线程(UI线程)就是一个消息循环的线程。针对这种消息循环的机制,我们引入一个新的机制Handler,我们有消息循环,就要往消息循环里面发送相应的消息,自定义消息一般都会有自己对应的处理,消息的发送和清除,把这些都封装在Handler里面,注意Handler只是针对那 些有Looper的线程,不管是UI线程还是子线程,只要你有Looper,我就可以往你的消息队列里面添加东西,并做相应的处理。
一个Handler的创建它就会被绑定到这个线程的消息队列中,如果是在主线程创建的,那就不需要写代码来创建消息队列了,默认的消息队列会在主线程被创建。但是如果是在子线程的话,就必须在创建Handler之前先初始化线程的消息队列。
实例如下:
package com.example.android_handle;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import org.apache.http.util.ByteArrayBuffer;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.method.ScrollingMovementMethod;
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 Handler mHandler;
private Button start;
private TextView text;
private int i = 0;
private Handler handler;
private NewThread thread = new NewThread();
private String s = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button) findViewById(R.id.start);
text = (TextView) findViewById(R.id.text);
text.setMovementMethod(ScrollingMovementMethod.getInstance());
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
s = s + msg.obj + "\n";
text.setText(s);
Message childMsg = mHandler.obtainMessage();
childMsg.obj = mHandler.getLooper().getThread()
.getName()
+ " says Hello";
mHandler.sendMessage(childMsg);
}
};
thread.start();
start.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mHandler != null) {
// 发送消息给子线程
Message childMsg = mHandler.obtainMessage();
childMsg.obj = mHandler.getLooper().getThread()
.getName()
+ " says Hello";
mHandler.sendMessage(childMsg);
}
}
});
}
private class NewThread extends Thread {
private Looper mLooper;
private String content;
public void run() {
Looper.prepare();
mLooper = Looper.myLooper();
mHandler = new Handler(mLooper) {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
try {
sleep(1000);
// URL uri = new URL("http://112.74.78.53/scut-lib/"
// + "library/hotReading.php");
// URLConnection ucon = uri.openConnection();
// ucon.setConnectTimeout(10000);
// InputStream is = ucon.getInputStream();
// BufferedInputStream bis = new BufferedInputStream(is);
// ByteArrayBuffer baf = new ByteArrayBuffer(100);
// int current = 0;
// while ((current = bis.read()) != -1) {
// baf.append((byte) current);
// }
// content = new String(baf.toByteArray(), "UTF-8");
} catch (Exception e) {
// content = e.toString();
}
i++;
Message msg1 = Message.obtain();
msg1.obj = i+"";
handler.sendMessage(msg1);
}
};
Looper.loop();
}
}
}
更加详细,请看Android中线程通讯类Handlerhttp://uule.iteye.com/blog/1705951
时间: 2024-08-04 23:19:40