- UID
- 16560
- 帖子
- 42
- 精華
- 0
- 積分
- 152
- e望
- 3 點
- e幣
- 31 元
- e脈
- 0 條
- 來自
- 澳大利亞
- 在線時間
- 69 小時
| 2# 發(fā)表于 2010-1-29 08:01 | 只看該作者 程序代碼如下,后面部分有代碼說明:
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.
- 29. /** Called when the activity is first created. */
- 30. @Override
- 31. public void onCreate(Bundle savedInstanceState) {
- 32. super.onCreate(savedInstanceState);
- 33. context = this.getApplicationContext();
- 34. LinearLayout layout = new LinearLayout(this);
- 35. layout.setOrientation(LinearLayout.VERTICAL);
- 36. btn = new Button(this);
- 37. btn.setId(101);
- 38. btn.setText("message from main thread self");
- 39. btn.setOnClickListener(this);
- 40. LinearLayout.LayoutParams param =
- 41. new LinearLayout.LayoutParams(250,50);
- 42. param.topMargin = 10;
- 43. layout.addView(btn, param);
- 44. btn2 = new Button(this);
- 45. btn2.setId(102);
- 46. btn2.setText("message from other thread to main thread");
- 47. btn2.setOnClickListener(this);
- 48. layout.addView(btn2, param);
- 49. btn3 = new Button(this);
- 50. btn3.setId(103);
- 51. btn3.setText("message to other thread from itself");
- 52. btn3.setOnClickListener(this);
- 53. layout.addView(btn3, param);
- 54. btn4 = new Button(this);
- 55. btn4.setId(104);
- 56. btn4.setText("message with Runnable as callback from other thread to main thread");
- 57. btn4.setOnClickListener(this);
- 58. layout.addView(btn4, param);
- 59. btn5 = new Button(this);
- 60. btn5.setId(105);
- 61. btn5.setText("main thread's message to other thread");
- 62. btn5.setOnClickListener(this);
- 63. layout.addView(btn5, param);
- 64. btn6 = new Button(this);
- 65. btn6.setId(106);
- 66. btn6.setText("exit");
- 67. btn6.setOnClickListener(this);
- 68. layout.addView(btn6, param);
- 69. tv = new TextView(this);
- 70. tv.setTextColor(Color.WHITE);
- 71. tv.setText("");
- 72. LinearLayout.LayoutParams param2 =
- 73. new LinearLayout.LayoutParams(FP, WC);
- 74. param2.topMargin = 10;
- 75. layout.addView(tv, param2);
- 76. setContentView(layout);
- 77.
- 78. //主線程要發(fā)送消息給other thread, 這里創(chuàng)建那個other thread
- 79. receiveMessageThread = new ReceiveMessageThread();
- 80. receiveMessageThread.start();
- 81. }
- 82.
- 83. //implement the OnClickListener interface
- 84. @Override
- 85. public void onClick(View v) {
- 86. switch(v.getId()){
- 87. case 101:
- 88. //主線程發(fā)送消息給自己
- 89. Looper looper;
- 90. looper = Looper.myLooper(); //get the Main looper related with the main thread
- 91. //如果不給任何參數(shù)的話會用當(dāng)前線程對應(yīng)的Looper(這里就是Main Looper)為Handler里面的成員mLooper賦值
- 92. mHandler = new EventHandler(looper);
- 93. //mHandler = new EventHandler();
- 94. // 清除整個MessageQueue里的消息
- 95. mHandler.removeMessages(0);
- 96. String obj = "This main thread's message and received by itself!";
- 97. //得到Message對象
- 98. Message m = mHandler.obtainMessage(1, 1, 1, obj);
- 99. // 將Message對象送入到main thread的MessageQueue里面
- 100. mHandler.sendMessage(m);
- 101. break;
- 102. case 102:
- 103. //other線程發(fā)送消息給主線程
- 104. postRunnable = false;
- 105. noLooerThread = new NoLooperThread();
- 106. noLooerThread.start();
- 107. break;
- 108. case 103:
- 109. //other thread獲取它自己發(fā)送的消息
- 110. tv.setText("please look at the error level log for other thread received message");
- 111. ownLooperThread = new OwnLooperThread();
- 112. ownLooperThread.start();
- 113. break;
- 114. case 104:
- 115. //other thread通過Post Runnable方式發(fā)送消息給主線程
- 116. postRunnable = true;
- 117. noLooerThread = new NoLooperThread();
- 118. noLooerThread.start();
- 119. break;
- 120. case 105:
- 121. //主線程發(fā)送消息給other thread
- 122. if(null!=mOtherThreadHandler){
- 123. tv.setText("please look at the error level log for other thread received message from main thread");
- 124. String msgObj = "message from mainThread";
- 125. Message mainThreadMsg = mOtherThreadHandler.obtainMessage(1, 1, 1, msgObj);
- 126. mOtherThreadHandler.sendMessage(mainThreadMsg);
- 127. }
- 128. break;
- 129. case 106:
- 130. finish();
- 131. break;
- 132. }
- 133. }
- 134. class EventHandler extends Handler
- 135. {
- 136. public EventHandler(Looper looper) {
- 137. super(looper);
- 138. }
- 139. public EventHandler() {
- 140. super();
- 141. }
- 142. public void handleMessage(Message msg) {
- 143. //可以根據(jù)msg.what執(zhí)行不同的處理,這里沒有這么做
- 144. switch(msg.what){
- 145. case 1:
- 146. tv.setText((String)msg.obj);
- 147. break;
- 148. case 2:
- 149. tv.setText((String)msg.obj);
- 150. noLooerThread.stop();
- 151. break;
- 152. case 3:
- 153. //不能在非主線程的線程里面更新UI,所以這里通過Log打印收到的消息
- 154. Log.e(sTag, (String)msg.obj);
- 155. ownLooperThread.stop();
- 156. break;
- 157. default:
- 158. //不能在非主線程的線程里面更新UI,所以這里通過Log打印收到的消息
- 159. Log.e(sTag, (String)msg.obj);
- 160. break;
- 161. }
- 162. }
- 163. }
- 164. //NoLooperThread
- 165. class NoLooperThread extends Thread{
- 166. private EventHandler mNoLooperThreadHandler;
- 167. public void run() {
- 168. Looper myLooper, mainLooper;
- 169. myLooper = Looper.myLooper();
- 170. mainLooper = Looper.getMainLooper(); //這是一個static函數(shù)
- 171. String obj;
- 172. if(myLooper == null){
- 173. mNoLooperThreadHandler = new EventHandler(mainLooper);
- 174. obj = "NoLooperThread has no looper and handleMessage function executed in main thread!";
- 175. }
- 176. else {
- 177. mNoLooperThreadHandler = new EventHandler(myLooper);
- 178. obj = "This is from NoLooperThread self and handleMessage function executed in NoLooperThread!";
- 179. }
- 180. mNoLooperThreadHandler.removeMessages(0);
- 181. if(false == postRunnable){
- 182. //send message to main thread
- 183. Message m = mNoLooperThreadHandler.obtainMessage(2, 1, 1, obj);
- 184. mNoLooperThreadHandler.sendMessage(m);
- 185. Log.e(sTag, "NoLooperThread id:" + this.getId());
- 186. }else{
- 187. //下面new出來的實現(xiàn)了Runnable接口的對象中run函數(shù)是在Main Thread中執(zhí)行,不是在NoLooperThread中執(zhí)行
- 188. //注意Runnable是一個接口,它里面的run函數(shù)被執(zhí)行時不會再新建一個線程
- 189. //您可以在run上加斷點然后在eclipse調(diào)試中看它在哪個線程中執(zhí)行
- 190. mNoLooperThreadHandler.post(new Runnable(){
- 191. @Override
- 192. public void run() {
- 193. tv.setText("update UI through handler post runnalbe mechanism!");
- 194. noLooerThread.stop();
- 195. }
- 196. });
- 197. }
- 198. }
- 199. }
- 200.
- 201. //OwnLooperThread has his own message queue by execute Looper.prepare();
- 202. class OwnLooperThread extends Thread{
- 203. private EventHandler mOwnLooperThreadHandler;
- 204. public void run() {
- 205. Looper.prepare();
- 206. Looper myLooper, mainLooper;
- 207. myLooper = Looper.myLooper();
- 208. mainLooper = Looper.getMainLooper(); //這是一個static函數(shù)
- 209. String obj;
- 210. if(myLooper == null){
- 211. mOwnLooperThreadHandler = new EventHandler(mainLooper);
- 212. obj = "OwnLooperThread has no looper and handleMessage function executed in main thread!";
- 213. }
- 214. else {
- 215. mOwnLooperThreadHandler = new EventHandler(myLooper);
- 216. obj = "This is from OwnLooperThread self and handleMessage function executed in NoLooperThread!";
- 217. }
- 218. mOwnLooperThreadHandler.removeMessages(0);
- 219. //給自己發(fā)送消息
- 220. Message m = mOwnLooperThreadHandler.obtainMessage(3, 1, 1, obj);
- 221. mOwnLooperThreadHandler.sendMessage(m);
- 222. Looper.loop();
- 223. }
- 224. }
- 225.
- 226. //ReceiveMessageThread has his own message queue by execute Looper.prepare();
- 227. class ReceiveMessageThread extends Thread{
- 228. public void run() {
- 229. Looper.prepare();
- 230. mOtherThreadHandler = new Handler(){
- 231. public void handleMessage(Message msg) {
- 232. Log.e(sTag, (String)msg.obj);
- 233. }
- 234. };
- 235. Looper.loop();
- 236. }
- 237. }
- 238.
- 239. }
- 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);
-
- //主線程要發(fā)送消息給other thread, 這里創(chuàng)建那個other thread
- receiveMessageThread = new ReceiveMessageThread();
- receiveMessageThread.start();
- }
- //implement the OnClickListener interface
- @Override
- public void onClick(View v) {
- switch(v.getId()){
- case 101:
- //主線程發(fā)送消息給自己
- Looper looper;
- looper = Looper.myLooper(); //get the Main looper related with the main thread
- //如果不給任何參數(shù)的話會用當(dāng)前線程對應(yīng)的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線程發(fā)送消息給主線程
- postRunnable = false;
- noLooerThread = new NoLooperThread();
- noLooerThread.start();
- break;
- case 103:
- //other thread獲取它自己發(fā)送的消息
- 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方式發(fā)送消息給主線程
- postRunnable = true;
- noLooerThread = new NoLooperThread();
- noLooerThread.start();
- break;
- case 105:
- //主線程發(fā)送消息給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) {
- //可以根據(jù)msg.what執(zhí)行不同的處理,這里沒有這么做
- 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函數(shù)
- 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出來的實現(xiàn)了Runnable接口的對象中run函數(shù)是在Main Thread中執(zhí)行,不是在NoLooperThread中執(zhí)行
- //注意Runnable是一個接口,它里面的run函數(shù)被執(zhí)行時不會再新建一個線程
- //您可以在run上加斷點然后在eclipse調(diào)試中看它在哪個線程中執(zhí)行
- 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函數(shù)
- 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);
- //給自己發(fā)送消息
- 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();
- }
- }
- }
復(fù)制代碼 說明(代碼詳細解釋請見后文):
使用Looper.myLooper靜態(tài)方法可以取得當(dāng)前線程的Looper對象。
使用mHandler = new EevntHandler(Looper.myLooper()); 可建立用來處理當(dāng)前線程的Handler對象;其中,EevntHandler是Handler的子類。
使用mHandler = new EevntHandler(Looper.getMainLooper()); 可建立用來處理main線程的Handler對象;其中,EevntHandler是Handler的子類。 | |