由別的線程送訊息到主線程的Message Queue 
Java代码  
  1. package com.example.ac01;  
  2.   
  3. import android.app.Activity;  
  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.view.View;  
  10. import android.view.View.OnClickListener;  
  11. import android.widget.Button;  
  12. import android.widget.LinearLayout;  
  13. import android.widget.TextView;  
  14.   
  15. //由別的線程送訊息到主線程的Message Queue  
  16. public class ac02 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 myThread t;  
  21.     private Button btn, btn2;  
  22.   
  23.     public void onCreate(Bundle icicle) {  
  24.         super.onCreate(icicle);  
  25.         LinearLayout layout = new LinearLayout(this);  
  26.         layout.setOrientation(LinearLayout.VERTICAL);  
  27.         btn = new Button(this);  
  28.         btn.setId(101);  
  29.   
  30.         btn.setBackgroundResource(R.drawable.icon);  
  31.         btn.setText("test looper");  
  32.         btn.setOnClickListener(this);  
  33.         LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(10050);  
  34.         param.topMargin = 10;  
  35.         layout.addView(btn, param);  
  36.         btn2 = new Button(this);  
  37.         btn2.setId(102);  
  38.         btn2.setBackgroundResource(R.drawable.icon);  
  39.         btn2.setText("exit");  
  40.         btn2.setOnClickListener(this);  
  41.         layout.addView(btn2, param);  
  42.         tv = new TextView(this);  
  43.         tv.setTextColor(Color.WHITE);  
  44.         tv.setText("");  
  45.         LinearLayout.LayoutParams param2 = new LinearLayout.LayoutParams(FP, WC);  
  46.         param2.topMargin = 10;  
  47.         layout.addView(tv, param2);  
  48.         setContentView(layout);  
  49.     }  
  50.   
  51.     public void onClick(View v) {  
  52.         switch (v.getId()) {  
  53.         case 101:  
  54.             t = new myThread();  
  55.             t.start();  
  56.             break;  
  57.         case 102:  
  58.             finish();  
  59.             break;  
  60.         }  
  61.     }  
  62.   
  63.     // ------------------------------------------------------  
  64.     class EHandler extends Handler {  
  65.         public EHandler(Looper looper) {  
  66.             super(looper);  
  67.         }  
  68.   
  69.         @Override  
  70.         public void handleMessage(Message msg) {  
  71.             tv.setText((String) msg.obj);  
  72.         }  
  73.     }  
  74.   
  75.     // ------------------------------------------------------  
  76.     class myThread extends Thread {  
  77.         private EHandler mHandler;  
  78.   
  79.         // Android會自動替主線程建立Message Queue  
  80.         // 在這個子線程裡並沒有建立Message Queue  
  81.         // 所以,myLooper值為null,而mainLooper則指向主線程裡的Looper物件  
  82.         public void run() {  
  83.             Looper myLooper, mainLooper;  
  84.             myLooper = Looper.myLooper();  
  85.             mainLooper = Looper.getMainLooper();  
  86.             String obj;  
  87.             if (myLooper == null) {  
  88.                 // 此mHandler屬於主線程  
  89.                 // 这里我们可以这样理解:因为根据demo1中的形象比喻,如果这个机构的传话员,跟这个Looper部门  
  90.                 // 应该属于同一个线程才符合情理  
  91.                 mHandler = new EHandler(mainLooper);  
  92.                 obj = "current thread has no looper!";  
  93.             } else {  
  94.                 mHandler = new EHandler(myLooper);  
  95.                 obj = "This is from current thread.";  
  96.             }  
  97.             mHandler.removeMessages(0);  
  98.             Message m = mHandler.obtainMessage(111, obj);  
  99.             mHandler.sendMessage(m);  
  100.         }  
  101.     }  
  102. }  
  103. // -------------------------------------------------------  


代码中已经做了很详细的解释,有疑问的我们可以一起探讨

/Files/foura/looper_2.rar

posted on 2011-04-15 10:05  kitea  阅读(147)  评论(0)    收藏  举报