Android线程间通信的Message机制
2010-06-01 15:44:00 来源:WEB开发网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. //给自己发送消息
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;
更多精彩
赞助商链接