原文出自:http://qaohao.javaeye.com/blog/509145

1.活用Android线程间通信的Message机制
1.1.Message
代码在frameworks/base/core/java/android/Os/Message.java中。
Message.obtain函数:有多个obtain函数,主要功能一样,只是参数不一样。作用是从Message Pool中取出一个Message,如果Message Pool中已经没有Message可取则新建一个Message返回,同时用对应的参数给得到的Message对象赋值。
Message Pool:大小为10个;通过Message.mPool->(Message并且Message.next)-> (Message并且Message.next)-> (Message并且Message.next)...构造一个Message Pool。Message Pool的第一个元素直接new出来,然后把Message.mPool(static类的static变量)指向它。其他的元素都是使用完的 Message通过Message的recycle函数清理后放到Message Pool(通过Message Pool最后一个Message的next指向需要回收的Message的方式实现)。下图为Message Pool的结构:

1.2.MessageQueue
MessageQueue里面有一个收到的Message的对列:
MessageQueue.mMessages(static变量)->( Message并且Message.next)-> ( Message并且Message.next)->...,下图为接收消息的消息队列:

上层代码通过Handler的sendMessage等函数放入一个message到MessageQueue里面时最终会调用MessageQueue的 enqueueMessage函数。enqueueMessage根据上面的接收的Message的队列的构造把接收到的Message放入队列中。
MessageQueue的removeMessages函数根据上面的接收的Message的队列的构造把接收到的Message从队列中删除,并且调用对应Message对象的recycle函数把不用的Message放入Message Pool中。
1.3.Looper
Looper对象的创建是通过prepare函数,而且每一个Looper对象会和一个线程关联

Java代码

  1. public static final void prepare() {
  2. if (sThreadLocal.get() != null) {
  3. throw new RuntimeException("Only one Looper may be created per thread");
  4. }
  5. sThreadLocal.set(new Looper());
  6. }
public static final void prepare() {    if (sThreadLocal.get() != null) {        throw new RuntimeException("Only one Looper may be created per thread");    }    sThreadLocal.set(new Looper());}

Looper对象创建时会创建一个MessageQueue,主线程默认会创建一个Looper从而有MessageQueue,其他线程默认是没有 MessageQueue的不能接收Message,如果需要接收Message则需要通过prepare函数创建一个MessageQueue。具体操作请见示例代码。

Java代码

  1. private Looper() {
  2. mQueue = new MessageQueue();
  3. mRun = true;
  4. mThread = Thread.currentThread();
  5. }
private Looper() {    mQueue = new MessageQueue();    mRun = true;    mThread = Thread.currentThread();}

prepareMainLooper函数只给主线程调用(系统处理,程序员不用处理),它会调用prepare建立Looper对象和MessageQueue。

Java代码

  1. public static final void prepareMainLooper() {
  2. prepare();
  3. setMainLooper(myLooper());
  4. if (Process.supportsProcesses()) {
  5. myLooper().mQueue.mQuitAllowed = false;
  6. }
  7. }
public static final void prepareMainLooper() {    prepare();    setMainLooper(myLooper());    if (Process.supportsProcesses()) {        myLooper().mQueue.mQuitAllowed = false;    }}

Loop函数从MessageQueue中从前往后取出Message,然后通过Handler的dispatchMessage函数进行消息的处理(可见消息的处理是Handler负责的),消息处理完了以后通过Message对象的recycle函数放到Message Pool中,以便下次使用,通过Pool的处理提供了一定的内存管理从而加速消息对象的获取。至于需要定时处理的消息如何做到定时处理,请见 MessageQueue的next函数,它在取Message来进行处理时通过判断MessageQueue里面的Message是否符合时间要求来决定是否需要把Message取出来做处理,通过这种方式做到消息的定时处理。

Java代码

  1. public static final void loop() {
  2. Looper me = myLooper();
  3. MessageQueue queue = me.mQueue;
  4. while (true) {
  5. Message msg = queue.next(); // might block
  6. //if (!me.mRun) {
  7. // break;
  8. //}
  9. if (msg != null) {
  10. if (msg.target == null) {
  11. // No target is a magic identifier for the quit message
  12. return;
  13. }
  14. if (me.mLogging!= null)
  15. me.mLogging.println(">>>>> Dispatching to " + msg.target + " "+ msg.callback + ": " + msg.what);
  16. msg.target.dispatchMessage(msg);
  17. if (me.mLogging!= null)
  18. me.mLogging.println("<<<<< Finished to" + msg.target + " "+ msg.callback);
  19. msg.recycle();
  20. }
  21. }
  22. }
public static final void loop() {    Looper me = myLooper();    MessageQueue queue = me.mQueue;    while (true) {        Message msg = queue.next(); // might block        //if (!me.mRun) {        //    break;        //}        if (msg != null) {            if (msg.target == null) {                // No target is a magic identifier for the quit message                return;            }            if (me.mLogging!= null)                 me.mLogging.println(">>>>> Dispatching to " + msg.target + " "+ msg.callback + ": " + msg.what);            msg.target.dispatchMessage(msg);            if (me.mLogging!= null)                 me.mLogging.println("<<<<< Finished to" + msg.target + " "+ msg.callback);            msg.recycle();        }    }}

1.4.Handler
Handler的构造函数表示Handler会有成员变量指向Looper和MessageQueue,后面我们会看到没什么需要这些引用;至于callback是实现了Callback接口的对象,后面会看到这个对象的作用。

Java代码

  1. public Handler(Looper looper, Callback callback) {
  2. mLooper = looper;
  3. mQueue = looper.mQueue;
  4. mCallback = callback;
  5. }
  6. public interface Callback {
  7. public boolean handleMessage(Message msg);
  8. }
public Handler(Looper looper, Callback callback) {    mLooper = looper;    mQueue = looper.mQueue;    mCallback = callback;}public interface Callback {    public boolean handleMessage(Message msg);}

获取消息:直接通过Message的obtain方法获取一个Message对象。

Java代码

  1. public final Message obtainMessage(int what, int arg1, int arg2, Object obj){
  2. return Message.obtain(this, what, arg1, arg2, obj);
  3. }
public final Message obtainMessage(int what, int arg1, int arg2, Object obj){    return Message.obtain(this, what, arg1, arg2, obj);}

发送消息:通过MessageQueue的enqueueMessage把Message对象放到MessageQueue的接收消息队列中

Java代码

  1. public boolean sendMessageAtTime(Message msg, long uptimeMillis){
  2. boolean sent = false;
  3. MessageQueue queue = mQueue;
  4. if (queue != null) {
  5. msg.target = this;
  6. sent = queue.enqueueMessage(msg, uptimeMillis);
  7. } else {
  8. RuntimeException e = new RuntimeException(this + " sendMessageAtTime() called with no mQueue");
  9. Log.w("Looper", e.getMessage(), e);
  10. }
  11. return sent;
  12. }
public boolean sendMessageAtTime(Message msg, long uptimeMillis){    boolean sent = false;    MessageQueue queue = mQueue;    if (queue != null) {        msg.target = this;    sent = queue.enqueueMessage(msg, uptimeMillis);    } else {        RuntimeException e = new RuntimeException(this + " sendMessageAtTime() called with no mQueue");        Log.w("Looper", e.getMessage(), e);    }    return sent;}

线程如何处理MessageQueue中接收的消息:在Looper的loop函数中循环取出MessageQueue的接收消息队列中的消息,然后调用 Hander的dispatchMessage函数对消息进行处理,至于如何处理(相应消息)则由用户指定(三个方法,优先级从高到低:Message里面的Callback,一个实现了Runnable接口的对象,其中run函数做处理工作;Handler里面的mCallback指向的一个实现了 Callback接口的对象,里面的handleMessage进行处理;处理消息Handler对象对应的类继承并实现了其中 handleMessage函数,通过这个实现的handleMessage函数处理消息)。

Java代码

  1. public void dispatchMessage(Message msg) {
  2. if (msg.callback != null) {
  3. handleCallback(msg);
  4. } else {
  5. if (mCallback != null) {
  6. if (mCallback.handleMessage(msg)) {
  7. return;
  8. }
  9. }
  10. handleMessage(msg);
  11. }
  12. }
public void dispatchMessage(Message msg) {    if (msg.callback != null) {        handleCallback(msg);    } else {        if (mCallback != null) {            if (mCallback.handleMessage(msg)) {                return;            }        }        handleMessage(msg);    }}

Runnable说明:Runnable只是一个接口,实现了这个接口的类对应的对象也只是个普通的对象,并不是一个Java中的Thread。Thread类经常使用Runnable,很多人有误解,所以这里澄清一下。
从上可知以下关系图:

其中清理Message是Looper里面的loop函数指把处理过的Message放到Message的Pool里面去,如果里面已经超过最大值10个,则丢弃这个Message对象。
调用Handler是指Looper里面的loop函数从MessageQueue的接收消息队列里面取出消息,然后根据消息指向的Handler对象调用其对应的处理方法。
1.5.代码示例
下面我们会以android实例来展示对应的功能,程序界面于下:

程序代码如下,后面部分有代码说明:

Java代码

  1. package com.android.messageexample;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.graphics.Color;
  5. import android.os.Bundle;
  6. import android.os.Handler;
  7. import android.os.Looper;
  8. import android.os.Message;
  9. import android.util.Log;
  10. import android.view.View;
  11. import android.view.View.OnClickListener;
  12. import android.widget.Button;
  13. import android.widget.LinearLayout;
  14. import android.widget.TextView;
  15. public class MessageExample extends Activity implements OnClickListener {
  16. private final int WC = LinearLayout.LayoutParams.WRAP_CONTENT;
  17. private final int FP = LinearLayout.LayoutParams.FILL_PARENT;
  18. public TextView tv;
  19. private EventHandler mHandler;
  20. private Handler mOtherThreadHandler=null;
  21. private Button btn, btn2, btn3, btn4, btn5, btn6;
  22. private NoLooperThread noLooerThread = null;
  23. private OwnLooperThread ownLooperThread = null;
  24. private ReceiveMessageThread receiveMessageThread =null;
  25. private Context context = null;
  26. private final String sTag = "MessageExample";
  27. private boolean postRunnable = false;
  28. /** Called when the activity is first created. */
  29. @Override
  30. public void onCreate(Bundle savedInstanceState) {
  31. super.onCreate(savedInstanceState);
  32. context = this.getApplicationContext();
  33. LinearLayout layout = new LinearLayout(this);
  34. layout.setOrientation(LinearLayout.VERTICAL);
  35. btn = new Button(this);
  36. btn.setId(101);
  37. btn.setText("message from main thread self");
  38. btn.setOnClickListener(this);
  39. LinearLayout.LayoutParams param =
  40. new LinearLayout.LayoutParams(250,50);
  41. param.topMargin = 10;
  42. layout.addView(btn, param);
  43. btn2 = new Button(this);
  44. btn2.setId(102);
  45. btn2.setText("message from other thread to main thread");
  46. btn2.setOnClickListener(this);
  47. layout.addView(btn2, param);
  48. btn3 = new Button(this);
  49. btn3.setId(103);
  50. btn3.setText("message to other thread from itself");
  51. btn3.setOnClickListener(this);
  52. layout.addView(btn3, param);
  53. btn4 = new Button(this);
  54. btn4.setId(104);
  55. btn4.setText("message with Runnable as callback from other thread to main thread");
  56. btn4.setOnClickListener(this);
  57. layout.addView(btn4, param);
  58. btn5 = new Button(this);
  59. btn5.setId(105);
  60. btn5.setText("main thread's message to other thread");
  61. btn5.setOnClickListener(this);
  62. layout.addView(btn5, param);
  63. btn6 = new Button(this);
  64. btn6.setId(106);
  65. btn6.setText("exit");
  66. btn6.setOnClickListener(this);
  67. layout.addView(btn6, param);
  68. tv = new TextView(this);
  69. tv.setTextColor(Color.WHITE);
  70. tv.setText("");
  71. LinearLayout.LayoutParams param2 =
  72. new LinearLayout.LayoutParams(FP, WC);
  73. param2.topMargin = 10;
  74. layout.addView(tv, param2);
  75. setContentView(layout);
  76. //主线程要发送消息给other thread, 这里创建那个other thread
  77. receiveMessageThread = new ReceiveMessageThread();
  78. receiveMessageThread.start();
  79. }
  80. //implement the OnClickListener interface
  81. @Override
  82. public void onClick(View v) {
  83. switch(v.getId()){
  84. case 101:
  85. //主线程发送消息给自己
  86. Looper looper;
  87. looper = Looper.myLooper(); //get the Main looper related with the main thread
  88. //如果不给任何参数的话会用当前线程对应的Looper(这里就是Main Looper)为Handler里面的成员mLooper赋值
  89. mHandler = new EventHandler(looper);
  90. //mHandler = new EventHandler();
  91. // 清除整个MessageQueue里的消息
  92. mHandler.removeMessages(0);
  93. String obj = "This main thread's message and received by itself!";
  94. //得到Message对象
  95. Message m = mHandler.obtainMessage(1, 1, 1, obj);
  96. // 将Message对象送入到main thread的MessageQueue里面
  97. mHandler.sendMessage(m);
  98. break;
  99. case 102:
  100. //other线程发送消息给主线程
  101. postRunnable = false;
  102. noLooerThread = new NoLooperThread();
  103. noLooerThread.start();
  104. break;
  105. case 103:
  106. //other thread获取它自己发送的消息
  107. tv.setText("please look at the error level log for other thread received message");
  108. ownLooperThread = new OwnLooperThread();
  109. ownLooperThread.start();
  110. break;
  111. case 104:
  112. //other thread通过Post Runnable方式发送消息给主线程
  113. postRunnable = true;
  114. noLooerThread = new NoLooperThread();
  115. noLooerThread.start();
  116. break;
  117. case 105:
  118. //主线程发送消息给other thread
  119. if(null!=mOtherThreadHandler){
  120. tv.setText("please look at the error level log for other thread received message from main thread");
  121. String msgObj = "message from mainThread";
  122. Message mainThreadMsg = mOtherThreadHandler.obtainMessage(1, 1, 1, msgObj);
  123. mOtherThreadHandler.sendMessage(mainThreadMsg);
  124. }
  125. break;
  126. case 106:
  127. finish();
  128. break;
  129. }
  130. }
  131. class EventHandler extends Handler
  132. {
  133. public EventHandler(Looper looper) {
  134. super(looper);
  135. }
  136. public EventHandler() {
  137. super();
  138. }
  139. public void handleMessage(Message msg) {
  140. //可以根据msg.what执行不同的处理,这里没有这么做
  141. switch(msg.what){
  142. case 1:
  143. tv.setText((String)msg.obj);
  144. break;
  145. case 2:
  146. tv.setText((String)msg.obj);
  147. noLooerThread.stop();
  148. break;
  149. case 3:
  150. //不能在非主线程的线程里面更新UI,所以这里通过Log打印收到的消息
  151. Log.e(sTag, (String)msg.obj);
  152. ownLooperThread.stop();
  153. break;
  154. default:
  155. //不能在非主线程的线程里面更新UI,所以这里通过Log打印收到的消息
  156. Log.e(sTag, (String)msg.obj);
  157. break;
  158. }
  159. }
  160. }
  161. //NoLooperThread
  162. class NoLooperThread extends Thread{
  163. private EventHandler mNoLooperThreadHandler;
  164. public void run() {
  165. Looper myLooper, mainLooper;
  166. myLooper = Looper.myLooper();
  167. mainLooper = Looper.getMainLooper(); //这是一个static函数
  168. String obj;
  169. if(myLooper == null){
  170. mNoLooperThreadHandler = new EventHandler(mainLooper);
  171. obj = "NoLooperThread has no looper and handleMessage function executed in main thread!";
  172. }
  173. else {
  174. mNoLooperThreadHandler = new EventHandler(myLooper);
  175. obj = "This is from NoLooperThread self and handleMessage function executed in NoLooperThread!";
  176. }
  177. mNoLooperThreadHandler.removeMessages(0);
  178. if(false == postRunnable){
  179. //send message to main thread
  180. Message m = mNoLooperThreadHandler.obtainMessage(2, 1, 1, obj);
  181. mNoLooperThreadHandler.sendMessage(m);
  182. Log.e(sTag, "NoLooperThread id:" + this.getId());
  183. }else{
  184. //下面new出来的实现了Runnable接口的对象中run函数是在Main Thread中执行,不是在NoLooperThread中执行
  185. //注意Runnable是一个接口,它里面的run函数被执行时不会再新建一个线程
  186. //您可以在run上加断点然后在eclipse调试中看它在哪个线程中执行
  187. mNoLooperThreadHandler.post(new Runnable(){
  188. @Override
  189. public void run() {
  190. tv.setText("update UI through handler post runnalbe mechanism!");
  191. noLooerThread.stop();
  192. }
  193. });
  194. }
  195. }
  196. }
  197. //OwnLooperThread has his own message queue by execute Looper.prepare();
  198. class OwnLooperThread extends Thread{
  199. private EventHandler mOwnLooperThreadHandler;
  200. public void run() {
  201. Looper.prepare();
  202. Looper myLooper, mainLooper;
  203. myLooper = Looper.myLooper();
  204. mainLooper = Looper.getMainLooper(); //这是一个static函数
  205. String obj;
  206. if(myLooper == null){
  207. mOwnLooperThreadHandler = new EventHandler(mainLooper);
  208. obj = "OwnLooperThread has no looper and handleMessage function executed in main thread!";
  209. }
  210. else {
  211. mOwnLooperThreadHandler = new EventHandler(myLooper);
  212. obj = "This is from OwnLooperThread self and handleMessage function executed in NoLooperThread!";
  213. }
  214. mOwnLooperThreadHandler.removeMessages(0);
  215. //给自己发送消息
  216. Message m = mOwnLooperThreadHandler.obtainMessage(3, 1, 1, obj);
  217. mOwnLooperThreadHandler.sendMessage(m);
  218. Looper.loop();
  219. }
  220. }
  221. //ReceiveMessageThread has his own message queue by execute Looper.prepare();
  222. class ReceiveMessageThread extends Thread{
  223. public void run() {
  224. Looper.prepare();
  225. mOtherThreadHandler = new Handler(){
  226. public void handleMessage(Message msg) {
  227. Log.e(sTag, (String)msg.obj);
  228. }
  229. };
  230. Looper.loop();
  231. }
  232. }
  233. }
package com.android.messageexample;import android.app.Activity;import android.content.Context;import android.graphics.Color;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.LinearLayout;import android.widget.TextView;public class MessageExample extends Activity implements OnClickListener { private final int WC = LinearLayout.LayoutParams.WRAP_CONTENT; private final int FP = LinearLayout.LayoutParams.FILL_PARENT; public TextView tv; private EventHandler mHandler; private Handler mOtherThreadHandler=null; private Button btn, btn2, btn3, btn4, btn5, btn6; private NoLooperThread noLooerThread = null; private OwnLooperThread ownLooperThread = null; private ReceiveMessageThread receiveMessageThread =null; private Context context = null; private final String sTag = "MessageExample"; private boolean postRunnable = false;  /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); context = this.getApplicationContext(); LinearLayout layout = new LinearLayout(this); layout.setOrientation(LinearLayout.VERTICAL); btn = new Button(this); btn.setId(101); btn.setText("message from main thread self"); btn.setOnClickListener(this); LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(250,50); param.topMargin = 10; layout.addView(btn, param); btn2 = new Button(this); btn2.setId(102); btn2.setText("message from other thread to main thread"); btn2.setOnClickListener(this); layout.addView(btn2, param); btn3 = new Button(this); btn3.setId(103); btn3.setText("message to other thread from itself"); btn3.setOnClickListener(this); layout.addView(btn3, param); btn4 = new Button(this); btn4.setId(104); btn4.setText("message with Runnable as callback from other thread to main thread"); btn4.setOnClickListener(this); layout.addView(btn4, param); btn5 = new Button(this); btn5.setId(105); btn5.setText("main thread's message to other thread"); btn5.setOnClickListener(this); layout.addView(btn5, param); btn6 = new Button(this); btn6.setId(106); btn6.setText("exit"); btn6.setOnClickListener(this); layout.addView(btn6, param); tv = new TextView(this); tv.setTextColor(Color.WHITE); tv.setText(""); LinearLayout.LayoutParams param2 = new LinearLayout.LayoutParams(FP, WC); param2.topMargin = 10; layout.addView(tv, param2); setContentView(layout); //主线程要发送消息给other thread, 这里创建那个other thread receiveMessageThread = new ReceiveMessageThread(); receiveMessageThread.start(); }  //implement the OnClickListener interface @Override public void onClick(View v) { switch(v.getId()){ case 101: //主线程发送消息给自己 Looper looper; looper = Looper.myLooper(); //get the Main looper related with the main thread //如果不给任何参数的话会用当前线程对应的Looper(这里就是Main Looper)为Handler里面的成员mLooper赋值 mHandler = new EventHandler(looper); //mHandler = new EventHandler(); // 清除整个MessageQueue里的消息 mHandler.removeMessages(0); String obj = "This main thread's message and received by itself!"; //得到Message对象 Message m = mHandler.obtainMessage(1, 1, 1, obj); // 将Message对象送入到main thread的MessageQueue里面 mHandler.sendMessage(m); break; case 102: //other线程发送消息给主线程 postRunnable = false; noLooerThread = new NoLooperThread(); noLooerThread.start(); break; case 103: //other thread获取它自己发送的消息 tv.setText("please look at the error level log for other thread received message"); ownLooperThread = new OwnLooperThread(); ownLooperThread.start(); break; case 104: //other thread通过Post Runnable方式发送消息给主线程 postRunnable = true; noLooerThread = new NoLooperThread(); noLooerThread.start(); break; case 105: //主线程发送消息给other thread if(null!=mOtherThreadHandler){ tv.setText("please look at the error level log for other thread received message from main thread"); String msgObj = "message from mainThread"; Message mainThreadMsg = mOtherThreadHandler.obtainMessage(1, 1, 1, msgObj); mOtherThreadHandler.sendMessage(mainThreadMsg); } break; case 106: finish(); break; } } class EventHandler extends Handler { public EventHandler(Looper looper) { super(looper); } public EventHandler() { super(); } public void handleMessage(Message msg) { //可以根据msg.what执行不同的处理,这里没有这么做 switch(msg.what){ case 1: tv.setText((String)msg.obj); break; case 2: tv.setText((String)msg.obj); noLooerThread.stop(); break; case 3: //不能在非主线程的线程里面更新UI,所以这里通过Log打印收到的消息 Log.e(sTag, (String)msg.obj); ownLooperThread.stop(); break; default: //不能在非主线程的线程里面更新UI,所以这里通过Log打印收到的消息 Log.e(sTag, (String)msg.obj); break; } } } //NoLooperThread class NoLooperThread extends Thread{ private EventHandler mNoLooperThreadHandler; public void run() { Looper myLooper, mainLooper; myLooper = Looper.myLooper(); mainLooper = Looper.getMainLooper(); //这是一个static函数 String obj; if(myLooper == null){ mNoLooperThreadHandler = new EventHandler(mainLooper); obj = "NoLooperThread has no looper and handleMessage function executed in main thread!"; } else { mNoLooperThreadHandler = new EventHandler(myLooper); obj = "This is from NoLooperThread self and handleMessage function executed in NoLooperThread!"; } mNoLooperThreadHandler.removeMessages(0); if(false == postRunnable){ //send message to main thread Message m = mNoLooperThreadHandler.obtainMessage(2, 1, 1, obj); mNoLooperThreadHandler.sendMessage(m); Log.e(sTag, "NoLooperThread id:" + this.getId()); }else{ //下面new出来的实现了Runnable接口的对象中run函数是在Main Thread中执行,不是在NoLooperThread中执行 //注意Runnable是一个接口,它里面的run函数被执行时不会再新建一个线程 //您可以在run上加断点然后在eclipse调试中看它在哪个线程中执行 mNoLooperThreadHandler.post(new Runnable(){ @Override public void run() { tv.setText("update UI through handler post runnalbe mechanism!"); noLooerThread.stop(); } }); } } }  //OwnLooperThread has his own message queue by execute Looper.prepare(); class OwnLooperThread extends Thread{ private EventHandler mOwnLooperThreadHandler; public void run() { Looper.prepare(); Looper myLooper, mainLooper; myLooper = Looper.myLooper(); mainLooper = Looper.getMainLooper(); //这是一个static函数 String obj; if(myLooper == null){ mOwnLooperThreadHandler = new EventHandler(mainLooper); obj = "OwnLooperThread has no looper and handleMessage function executed in main thread!"; } else { mOwnLooperThreadHandler = new EventHandler(myLooper); obj = "This is from OwnLooperThread self and handleMessage function executed in NoLooperThread!"; } mOwnLooperThreadHandler.removeMessages(0); //给自己发送消息 Message m = mOwnLooperThreadHandler.obtainMessage(3, 1, 1, obj); mOwnLooperThreadHandler.sendMessage(m); Looper.loop(); } }  //ReceiveMessageThread has his own message queue by execute Looper.prepare(); class ReceiveMessageThread extends Thread{ public void run() { Looper.prepare(); mOtherThreadHandler = new Handler(){ public void handleMessage(Message msg) { Log.e(sTag, (String)msg.obj); } }; Looper.loop(); } } }

说明(代码详细解释请见后文):
使用Looper.myLooper静态方法可以取得当前线程的Looper对象。
使用mHandler = new EevntHandler(Looper.myLooper()); 可建立用来处理当前线程的Handler对象;其中,EevntHandler是Handler的子类。
使用mHandler = new EevntHandler(Looper.getMainLooper()); 可建立用来处理main线程的Handler对象;其中,EevntHandler是Handler的子类。
1.5.1.主线程给自己发送消息示例
主线程发送消息:
在onClick的case 101中创建一个继承自Handler的EventHandler对象,然后获取一个消息,然后通过EventHandler对象调用 sendMessage把消息发送到主线程的MessageQueue中。主线程由系统创建,系统会给它建立一个Looper对象和 MessageQueue,所以可以接收消息。这里只要根据主线程的Looper对象初始化EventHandler对象,就可以通过 EventHandler对象发送消息到主线程的消息队列中。
主线程处理消息:
这里是通过EventHandler的handleMessage函数处理的,其中收到的Message对象中what值为一的消息就是发送给它的,然后把消息里面附带的字符串在TextView上显示出来。
1.5.2.其他线程给主线程发送消息示例
其他线程发送消息(这里是说不使用Runnable作为callback的消息):
首先 postRunnable设为false,表示不通过Runnable方式进行消息相关的操作。然后启动线程noLooerThread,然后以主线程的Looper对象为参数建立EventHandler的对象mNoLooperThreadHandler,然后获取一个Message并把一个字符串赋值给它的一个成员obj,然后通过mNoLooperThreadHandler把消息发送到主线程的MessageQueue中。
主线程处理消息:
这里是通过EventHandler的handleMessage函数处理的,其中收到的Message对象中what值为二的消息就是上面发送给它的,然后把消息里面附带的字符串在TextView上显示出来。
1.5.3.其他线程给自己发送消息示例
其他线程发送消息:
其他非主线程建立后没有自己的Looper对象,所以也没有MessageQueue,需要给非主线程发送消息时需要建立MessageQueue以便接收消息。下面说明如何给自己建立MessageQueue和Looper对象。从OwnLooperThread的run函数中可以看见有一个 Looper.prepare()调用,这个就是用来建立非主线程的MessageQueue和Looper对象的。
所以这里的发送消息过程是建立线程mOwnLooperThread,然后线程建立自己的Looper和MessageQueue对象,然后根据上面建立的Looper对象建立对应的EventHandler对象mOwnLooperThreadHandler,然后由mOwnLooperThreadHandler建立消息并且发送到自己的MessageQueue里面。
其他线程处理接收的消息:
线程要接收消息需要在run函数中调用Looper.loop(),然后loop函数会从MessageQueue中取出消息交给对应的Handler对象 mOwnLooperThreadHandler处理,在mOwnLooperThreadHandler的handleMessage函数中会把 Message对象中what值为三的消息(上面发送的消息)在Log中打印出来,可以通过Logcat工具查看log。
1.5.4.其他线程以Runnable为消息参数给主线程发送消息示例
其他线程发送消息(这里是说使用Runnable作为callback的消息):
首先 postRunnable设为true,表示通过Runnable方式进行消息相关的操作。然后启动线程noLooerThread,然后以主线程的Looper对象为参数建立EventHandler的对象mNoLooperThreadHandler,然后获取一个Message并把一个字符串赋值给它的一个成员obj,然后通过mNoLooperThreadHandler把消息发送到主线程的MessageQueue中。
主线程处理消息:
主线程收到上面发送的Message后直接运行上面Runnable对象中的run函数进行相应的操作。run函数通过Log打印一个字符串,可以通过Logcat工具查看log。
1.5.5.主线程给其他线程发送消息示例
主线程发送消息:
这里首先要求线程receiveMessageThread运行(在onCreate函数中完成),并且准备好自己的Looper和 MessageQueue(这个通过ReceiveMessageThread中的run函数中的Looper.prepare()调用完成),然后根据建立的Looper对象初始化Handler对象mOtherThreadHandler。然后在onClick的case 105中由mOtherThreadHandler建立一个消息(消息中有一个字符串对象)并且发送到线程receiveMessageThread中的 MessageQueue中。
其他线程处理接收的消息:
线程要接收消息需要在run函数中调用Looper.loop(),然后loop函数会从MessageQueue中取出消息交给对应的Handler对象mOtherThreadHandler处理,在mOtherThreadHandler的handleMessage函数中会把Message对象中的字符串对象在Log中打印出来,可以通过Logcat工具查看log。

更多相关文章

  1. Android多平台推送
  2. Android中对Handle机制的理解
  3. Android(安卓)HAL分析报告
  4. Android(安卓)framework源码按键操作的完整流程
  5. Android(安卓)中文API (91) ―― GestureDetector
  6. Unable to instantiate activity ComponentInfo
  7. 使用 kotlin 开发 android 遇到的问题
  8. Android消息通知
  9. 箭头函数的基础使用

随机推荐

  1. Android数据本地安全存储
  2. Android关于第三方h5在webview调用摄像头
  3. Android简单、高性能的高斯模糊(毛玻璃)效
  4. Android实现竖着的滑动刻度尺效果,选择身
  5. 一个3000万日活跃用户App的真实数据
  6. ubuntu 10.10 64Bit下编译android和andro
  7. Android(安卓)Mms专题之:信息发送流程
  8. Android:创建可穿戴应用 - 建立模拟器和创
  9. Android高效率编码-第三方SDK详解系列(二
  10. Android中实现推送方式