近来找了一些关于android线程间通讯的资料,整理学习了一下,并制做了一个简单的例子。java
andriod提供了 Handler 和 Looper 来知足线程间的通讯。例如一个子线程从网络上下载了一副图片,当它下载完成后会发送消息给主线程,这个消息是经过绑定在主线程的Handler来传递的。android
在Android,这里的线程分为有消息循环的线程和没有消息循环的线程,有消息循环的线程通常都会有一个Looper,这个是android的新概念。咱们的主线程(UI线程)就是一个消息循环的线程。针对这种消息循环的机制,咱们引入一个新的机制Handler,咱们有消息循环,就要往消息循环里面发送相应的消息,自定义消息通常都会有本身对应的处理,消息的发送和清除,把这些都封装在Handler里面,注意Handler只是针对那 些有Looper的线程,无论是UI线程仍是子线程,只要你有Looper,我就能够往你的消息队列里面添加东西,并作相应的处理。网络
可是这里还有一点,就是只要是关于UI相关的东西,就不能放在子线程中,由于子线程是不能操做UI的,只能进行数据、系统等其余非UI的操做。ide
一个Handler的建立它就会被绑定到这个线程的消息队列中,若是是在主线程建立的,那就不须要写代码来建立消息队列了,默认的消息队列会在主线程被建立。可是若是是在子线程的话,就必须在建立Handler以前先初始化线程的消息队列。以下面的代码:函数
class ChildThread extends Thread { public void run() { /* * 建立 handler前先初始化Looper. */ Looper.prepare(); /* * 在子线程建立handler,因此会绑定到子线程的消息队列中 * */ mChildHandler = new Handler() { public void handleMessage(Message msg) { /* * Do some expensive operations there. */ } }; /* * 启动该线程的消息队列 */ Looper.loop(); } }
当Handler收到消息后,就会运行handleMessage(…)的回调函数,能够在里面作一些耗时的操做。oop
最后完成了操做要结束子线程时,记得调用quit()来结束消息循环队列。学习
mChildHandler.getLooper().quit();
下面是一个线程间通讯的小例子:ui
/** * * @author allin.dev * http://allin.cnblogs.com * */ public class MainThread extends Activity { private static final String TAG = "MainThread"; private Handler mMainHandler, mChildHandler; private TextView info; private Button msgBtn; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); info = (TextView) findViewById(R.id.info); msgBtn = (Button) findViewById(R.id.msgBtn); mMainHandler = new Handler() { @Override public void handleMessage(Message msg) { Log.i(TAG, "Got an incoming message from the child thread - " + (String) msg.obj); // 接收子线程的消息 info.setText((String) msg.obj); } }; new ChildThread().start(); msgBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (mChildHandler != null) { //发送消息给子线程 Message childMsg = mChildHandler.obtainMessage(); childMsg.obj = mMainHandler.getLooper().getThread().getName() + " says Hello"; mChildHandler.sendMessage(childMsg); Log.i(TAG, "Send a message to the child thread - " + (String)childMsg.obj); } } }); } public void onDestroy() { super.onDestroy(); Log.i(TAG, "Stop looping the child thread's message queue"); mChildHandler.getLooper().quit(); } class ChildThread extends Thread { private static final String CHILD_TAG = "ChildThread"; public void run() { this.setName("ChildThread"); //初始化消息循环队列,须要在Handler建立以前 Looper.prepare(); mChildHandler = new Handler() { @Override public void handleMessage(Message msg) { Log.i(CHILD_TAG, "Got an incoming message from the main thread - " + (String)msg.obj); try { //在子线程中能够作一些耗时的工做 sleep(100); Message toMain = mMainHandler.obtainMessage(); toMain.obj = "This is " + this.getLooper().getThread().getName() + ". Did you send me \"" + (String)msg.obj + "\"?"; mMainHandler.sendMessage(toMain); Log.i(CHILD_TAG, "Send a message to the main thread - " + (String)toMain.obj); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }; Log.i(CHILD_TAG, "Child handler is bound to - "+ mChildHandler.getLooper().getThread().getName()); //启动子线程消息循环队列 Looper.loop(); } } }
[源码下载]this