Android的消息机制(2)
2011-12-30 11:45 tang768168 阅读(176) 评论(0) 编辑 收藏 举报上一节中,是主线程自己发了一个消息到自己的Message Queue中,并把消息从队列中提取出来。那么如何由别的线程发送消息给主线程的Message Queue中呢?
直接看代码~~
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
<span style="font-size: 14px">/** * MessageQueue2.java * com.test * * Function: TODO * * ver date author * ────────────────────────────────── * 2011-3-20 Leon * * Copyright (c) 2011, TNT All Rights Reserved. */ package com.test; /** * ClassName:MessageQueue2 * Function: TODO ADD FUNCTION * Reason: TODO ADD REASON * * @author Leon * @version * @since Ver 1.1 * @Date 2011-3-20 */ import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; public class MessageQueue2 extends Activity implements OnClickListener { private final int WC = LinearLayout.LayoutParams.WRAP_CONTENT; private final int FP = LinearLayout.LayoutParams.FILL_PARENT; public TextView tv; private myThread t; private Button btn, btn2, btn3; public void onCreate(Bundle icicle) { super.onCreate(icicle); LinearLayout layout = new LinearLayout(this); layout.setOrientation(LinearLayout.VERTICAL); btn = new Button(this); btn.setId(101); btn.setText("test looper"); btn.setOnClickListener(this); LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(100,50); param.topMargin = 10; layout.addView(btn, param); btn2 = new Button(this); btn2.setId(102); btn2.setText("exit"); btn2.setOnClickListener(this); layout.addView(btn2, 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); } public void onClick(View v) { switch(v.getId()){ case 101: t = new myThread(); t.start(); break; case 102: finish(); break; } } //------------------------------------------------------ class EHandler extends Handler { public EHandler(Looper looper) { super(looper); } @Override public void handleMessage(Message msg) { tv.setText((String)msg.obj); } } //------------------------------------------------------ class myThread extends Thread{ private EHandler mHandler; public void run() { Looper myLooper, mainLooper; //取出本线程中的Looper,当然如果本句话放在主线程中,则会取得主线程的Looper myLooper = Looper.myLooper(); //取出主线程中的Looper mainLooper = Looper.getMainLooper(); String obj; if(myLooper == null){ mHandler = new EHandler(mainLooper); obj = "current thread has no looper,this message is from Main thread!"; } else { mHandler = new EHandler(myLooper); obj = "This is from new thread."; } mHandler.removeMessages(0); Message m = mHandler.obtainMessage(1, 1, 1, obj); mHandler.sendMessage(m); } } } </span> |
在本程序中
Android仍然会自动替主线程建立Message Queue。在点击按钮之后,新生成的子线程中并不会建立Message
Queue。所以,新线程中的myLooper值为null,而mainLooper则指向主线程里的Looper对象。于是,执行当到指令:
mHandler = new EHandler(mainLooper); 时这个mHandler是属于主线程的。
通过指令:mHandler.sendMessage(m); 便将消息m存入到主线程的Message
Queue里。mainLooper看到Message
Queue里有讯息,就会自动处理,主线程会自动回调mHandler的handleMessage()函数来处理消息。
转载自最牛网
本文链接地址: Android的消息机制(2)