在J2ME中,多线程的处理是很简单的,似乎并不用考虑很多问题,顶多是同步方面很让人头疼,而在Android中,一切似乎不那么明了了,很重要的一点是在其他线程中是不能直接访问主UI线程成员的。比如说从网上获取一个网页,在一个TextView中将其源代码显示出来,这种涉及到网络操作的程序一般都是需要开一个线程完成网络访问,但是在获得页面源码后,是不能直接在网络操作线程中调用TextView.setText()的,在C#中也是这样,可能是为了主线程的安全吧。
对于此类情况,Android提供了一些间接访问的方法。
1.runOnUiThread函数
这个函数是Activity的成员函数,格式是public final void runOnUiThread (Runnable action)
在自定义线程中执行对应Activity的runOnUiThread函数就好了,但是很明显,参数还是个线程,这个
函数基本上没什么意义。
2.Handler
这应该是Android多线程中用的最多的工具了,这里Android借鉴了Windows的消息机制,不同线程之间可以用消息进行交流。除了主线程(UI线程)外,通过继承Thread或Runnable生成的线程是不包含消息循环(Looper)的。因此必须在线程中调用Looper.prepare()准备一个消息循环,调用Looper.loop()开始循环,就像是这样:
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();
}
}
这其中Handler是用来接受并处理消息的,如果像上面写的那样调用无参数构造函数生成handler,这个Handler对应的就是所在线程,也可以用Looper做参数,这样生成的Handler对应的是Looper所在的线程。可以通过Looper.myLooper()获取当前线程的Looper。调用Handler对象的sendMessage(Message)函数可以向对应线程发送消息。
例:
public class ThreadMain extends Activity {
/** Called when the activity is first created. */
private final static int MSG_BEGIN=1;
private final static int MSG_END=2;
private ProgressDialog dialog=null;
private Button bt=null;
private Handler handle=new Handler(){
public void handleMessage(Message msg){
switch(msg.what){
case MSG_BEGIN:
showProgress();
break;
case MSG_END:
dismissProgress();
alertDialog();
break;
}
super.handleMessage(msg);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bt=(Button)findViewById(R.id.bt);
bt.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
doThread();
}});
}
private void doThread(){
new Thread(){
public void run(){
try{
sendMsg(MSG_BEGIN);
Thread.sleep(4000);
sendMsg(MSG_END);
}catch(Exception e){
e