欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

ndroid中Message機(jī)制的靈活應(yīng)用(二)

系統(tǒng) 1717 0
1.5. 代碼示例

下面我們會(huì)以android實(shí)例來(lái)展示對(duì)應(yīng)的功能,程序界面于下:

下載 (9.5 KB)
2009-9-25 20:57


程序代碼如下,后面部分有代碼說(shuō)明:


說(shuō)明(代碼詳細(xì)解釋請(qǐng)見(jiàn)后文):

  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. //主線程要發(fā)送消息給other thread, 這里創(chuàng)建那個(gè)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. //主線程發(fā)送消息給自己
  86. Looper looper;
  87. looper = Looper.myLooper(); //get the Main looper related with the main thread
  88. //如果不給任何參數(shù)的話(huà)會(huì)用當(dāng)前線程對(duì)應(yīng)的Looper(這里就是Main Looper)為Handler里面的成員mLooper賦值
  89. mHandler = new EventHandler(looper);
  90. //mHandler = new EventHandler();
  91. // 清除整個(gè)MessageQueue里的消息
  92. mHandler.removeMessages(0);
  93. String obj = "This main thread's message and received by itself!";
  94. //得到Message對(duì)象
  95. Message m = mHandler.obtainMessage(1, 1, 1, obj);
  96. // 將Message對(duì)象送入到main thread的MessageQueue里面
  97. mHandler.sendMessage(m);
  98. break;
  99. case 102:
  100. //other線程發(fā)送消息給主線程
  101. postRunnable = false;
  102. noLooerThread = new NoLooperThread();
  103. noLooerThread.start();
  104. break;
  105. case 103:
  106. //other thread獲取它自己發(fā)送的消息
  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通過(guò)Post Runnable方式發(fā)送消息給主線程
  113. postRunnable = true;
  114. noLooerThread = new NoLooperThread();
  115. noLooerThread.start();
  116. break;
  117. case 105:
  118. //主線程發(fā)送消息給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. //可以根據(jù)msg.what執(zhí)行不同的處理,這里沒(méi)有這么做
  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,所以這里通過(guò)Log打印收到的消息
  151. Log.e(sTag, (String)msg.obj);
  152. ownLooperThread.stop();
  153. break;
  154. default:
  155. //不能在非主線程的線程里面更新UI,所以這里通過(guò)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(); //這是一個(gè)static函數(shù)
  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出來(lái)的實(shí)現(xiàn)了Runnable接口的對(duì)象中run函數(shù)是在Main Thread中執(zhí)行,不是在NoLooperThread中執(zhí)行
  185. //注意Runnable是一個(gè)接口,它里面的run函數(shù)被執(zhí)行時(shí)不會(huì)再新建一個(gè)線程
  186. //您可以在run上加斷點(diǎn)然后在eclipse調(diào)試中看它在哪個(gè)線程中執(zhí)行
  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(); //這是一個(gè)static函數(shù)
  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. //給自己發(fā)送消息
  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. }


使用Looper.myLooper靜態(tài)方法可以取得當(dāng)前線程的Looper對(duì)象。

使用mHandler = new EevntHandler(Looper.myLooper()); 可建立用來(lái)處理當(dāng)前線程的Handler對(duì)象;其中,EevntHandler是Handler的子類(lèi)。

使用mHandler = new EevntHandler(Looper.getMainLooper()); 可建立用來(lái)處理main線程的Handler對(duì)象;其中,EevntHandler是Handler的子類(lèi)。
1.5.1. 主線程給自己發(fā)送消息示例

主線程發(fā)送消息

onClick 的case 101中創(chuàng)建一個(gè)繼承自Handler的EventHandler對(duì)象,然后獲取一個(gè)消息,然后通過(guò)EventHandler對(duì)象調(diào)用 sendMessage把消息發(fā)送到主線程的MessageQueue中。主線程由系統(tǒng)創(chuàng)建,系統(tǒng)會(huì)給它建立一個(gè)Looper對(duì)象和 MessageQueue,所以可以接收消息。這里只要根據(jù)主線程的Looper對(duì)象初始化EventHandler對(duì)象,就可以通過(guò) EventHandler對(duì)象發(fā)送消息到主線程的消息隊(duì)列中。

主線程處理消息:

這里是通過(guò)EventHandler的handleMessage函數(shù)處理的,其中收到的Message對(duì)象中what值為一的消息就是發(fā)送給它的,然后把消息里面附帶的字符串在TextView上顯示出來(lái)。
1.5.2. 其他線程給主線程發(fā)送消息示例

其他線程發(fā)送消息(這里是說(shuō)不使用Runnable作為callback的消息):

首先 postRunnable設(shè)為false,表示不通過(guò)Runnable方式進(jìn)行消息相關(guān)的操作。然后啟動(dòng)線程noLooerThread, 然后以主線程的Looper對(duì)象為參數(shù)建立EventHandler的對(duì)象mNoLooperThreadHandler,然后獲取一個(gè)Message并 把一個(gè)字符串賦值給它的一個(gè)成員obj,然后通過(guò)mNoLooperThreadHandler把消息發(fā)送到主線程的MessageQueue中。

主線程處理消息:

這里是通過(guò)EventHandler的handleMessage函數(shù)處理的,其中收到的Message對(duì)象中what值為二的消息就是上面發(fā)送給它的,然后把消息里面附帶的字符串在TextView上顯示出來(lái)。
1.5.3. 其他線程給自己發(fā)送消息示例

其他線程發(fā)送消息:

其他非主線程建立后 沒(méi)有自己的Looper對(duì)象,所以也沒(méi)有MessageQueue,需要給非主線程發(fā)送消息時(shí)需要建立MessageQueue以便接收消息。下面說(shuō)明如 何給自己建立MessageQueue和Looper對(duì)象。從OwnLooperThread的run函數(shù)中可以看見(jiàn)有一個(gè) Looper.prepare()調(diào)用,這個(gè)就是用來(lái)建立非主線程的MessageQueue和Looper對(duì)象的。

所以這里的發(fā)送消息 過(guò)程是建立線程mOwnLooperThread,然后線程建立自己的Looper和MessageQueue對(duì)象,然后根據(jù)上面建立的Looper對(duì)象 建立對(duì)應(yīng)的EventHandler對(duì)象mOwnLooperThreadHandler,然后由mOwnLooperThreadHandler建立消 息并且發(fā)送到自己的MessageQueue里面。

其他線程處理接收的消息:

線程要接收消息需要 在run函數(shù)中調(diào)用Looper.loop(),然后loop函數(shù)會(huì)從MessageQueue中取出消息交給對(duì)應(yīng)的Handler對(duì)象 mOwnLooperThreadHandler處理,在mOwnLooperThreadHandler的handleMessage函數(shù)中會(huì)把 Message對(duì)象中what值為三的消息(上面發(fā)送的消息)在Log中打印出來(lái),可以通過(guò)Logcat工具查看log。
1.5.4. 其他線程以 Runnable 為消息參數(shù)給主線程發(fā)送消息示例

其他線程發(fā)送消息(這里是說(shuō)使用Runnable作為callback的消息):

首先 postRunnable設(shè)為true,表示通過(guò)Runnable方式進(jìn)行消息相關(guān)的操作。然后啟動(dòng)線程noLooerThread, 然后以主線程的Looper對(duì)象為參數(shù)建立EventHandler的對(duì)象mNoLooperThreadHandler,然后獲取一個(gè)Message并 把一個(gè)字符串賦值給它的一個(gè)成員obj,然后通過(guò)mNoLooperThreadHandler把消息發(fā)送到主線程的MessageQueue中。

主線程處理消息:

主線程收到上面發(fā)送的Message后直接運(yùn)行上面Runnable對(duì)象中的run函數(shù)進(jìn)行相應(yīng)的操作。run函數(shù)通過(guò)Log打印一個(gè)字符串,可以通過(guò)Logcat工具查看log。
1.5.5. 主線程給其他線程發(fā)送消息示例

主線程發(fā)送消息

這 里首先要求線程receiveMessageThread運(yùn)行(在onCreate函數(shù)中完成),并且準(zhǔn)備好自己的Looper和 MessageQueue(這個(gè)通過(guò)ReceiveMessageThread中的run函數(shù)中的Looper.prepare()調(diào)用完成),然后根據(jù) 建立的Looper對(duì)象初始化Handler對(duì)象mOtherThreadHandler。然后在onClick的case 105中由mOtherThreadHandler建立一個(gè)消息(消息中有一個(gè)字符串對(duì)象)并且發(fā)送到線程receiveMessageThread中的 MessageQueue中。

其他線程處理接收的消息:

線程要接收消息需要在run函數(shù)中調(diào)用Looper.loop(),然后loop函數(shù)會(huì)從MessageQueue中取出消息交給對(duì)應(yīng)的Handler對(duì)象 mOtherThreadHandler 處理,在 mOtherThreadHandler 的handleMessage函數(shù)中會(huì)把Message對(duì)象中的字符串對(duì)象在Log中打印出來(lái),可以通過(guò)Logcat工具查看log。

ndroid中Message機(jī)制的靈活應(yīng)用(二)


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 国内精品视频在线观看 | 成人精品视频在线观看 | 欧美日韩综合一区 | 999久久久国产精品 成人不卡视频 | 99久9在线 | 免费 | 香港三级日本三级韩国三级韩 | 天天操夜夜操天天操 | 久久伊人免费视频 | 五月伊人婷婷 | 2021国产精品自产拍在线观看 | 狠狠色依依成人婷婷九月 | 国产在线精品一区二区三区 | 欧美一级二级视频 | 午夜一级毛片 | 日本中文在线 | 尤物视频在线观看 | 日日夜夜天天 | 久久狠狠| 啪啪乐视频 | 天天狠狠 | 久久国产美女 | 欧美日韩在线免费观看 | 成人福利视频网 | 色汉综合 | 亚洲精品午夜国产va久久成人 | 久久久国产99久久国产首页 | 日韩在线免费观看视频 | 大学门卫老秦无删减版txt | 午夜大片免费男女爽爽影院久久 | 日韩欧美在线视频 | 欧美性猛交xxxx乱大交蜜桃 | 亚洲宗合| 夭天曰天天躁天天摸在线观看 | 亚洲国产精品第一区二区三区 | 久热国产在线视频 | 久久这里只有精品国产99 | 两性欧美| 国产成人精品久久二区二区 | 青青草99 | 精品国产精品国产 | 亚洲 欧美 日韩中文字幕一区二区 |