1. 程式人生 > >android通話模組詳解

android通話模組詳解

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