这个例子是为了更进一步的理解Demo2 
这个例子跟Demo2只是有些小的改动,大家可以对比的看下 
Java代码  
  1. package com.example.Looper_03;  
  2.   
  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.view.View;  
  11. import android.view.View.OnClickListener;  
  12. import android.widget.Button;  
  13. import android.widget.LinearLayout;  
  14. import android.widget.TextView;  
  15.   
  16. public class Looper_03 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.     EventHandler h;  
  23.     Context ctx;  
  24.   
  25.     public void onCreate(Bundle icicle) {  
  26.         super.onCreate(icicle);  
  27.         ctx = this;  
  28.         LinearLayout layout = new LinearLayout(this);  
  29.         layout.setOrientation(LinearLayout.VERTICAL);  
  30.         btn = new Button(this);  
  31.         btn.setId(101);  
  32.         btn.setBackgroundResource(R.drawable.icon);  
  33.         btn.setText("test looper");  
  34.         btn.setOnClickListener(this);  
  35.         LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(10050);  
  36.         param.topMargin = 10;  
  37.         layout.addView(btn, param);  
  38.         btn2 = new Button(this);  
  39.         btn2.setId(102);  
  40.         btn2.setBackgroundResource(R.drawable.icon);  
  41.         btn2.setText("exit");  
  42.         btn2.setOnClickListener(this);  
  43.         layout.addView(btn2, param);  
  44.         tv = new TextView(this);  
  45.         tv.setTextColor(Color.WHITE);  
  46.         tv.setText("");  
  47.         LinearLayout.LayoutParams param2 = new LinearLayout.LayoutParams(FP, WC);  
  48.         param2.topMargin = 10;  
  49.         layout.addView(tv, param2);  
  50.         setContentView(layout);  
  51.     }  
  52.   
  53.     public void onClick(View v) {  
  54.         switch (v.getId()) {  
  55.         case 101:  
  56.             // 此h是屬於main線程的(用來存取主線程的MessageQueue)  
  57.             //h = new EventHandler(Looper.myLooper());//@  
  58.               
  59.             //或者可以写成这样;  
  60.             h = new EventHandler();  
  61.               
  62.             t = new myThread();  
  63.             t.start();  
  64.             break;  
  65.         case 102:  
  66.             finish();  
  67.             break;  
  68.         }  
  69.     }  
  70.       
  71.     //或者可以写成这样;  
  72.     public class EventHandler extends Handler {  
  73.         @Override  
  74.         public void handleMessage(Message msg) {  
  75.             ((Activity) ctx).setTitle((String) msg.obj);  
  76.         }  
  77.     }  
  78.   
  79.     // ------------------------------------------------  
  80.     /*public class EventHandler extends Handler {//@ 
  81.         public EventHandler(Looper looper) { 
  82.             super(looper); 
  83.         } 
  84.  
  85.         @Override 
  86.         public void handleMessage(Message msg) { 
  87.             ((Activity) ctx).setTitle((String) msg.obj); 
  88.         } 
  89.     }*/  
  90.   
  91.     // ------------------------------------------------------  
  92.     class myThread extends Thread {  
  93.         public void run() {  
  94.             String obj = "from myThread";  
  95.             Message m = h.obtainMessage(111, obj);  
  96.             //这里还是通过主线程的handler来传送消息  
  97.             //所以还是由主线程的Looper来接受消息  
  98.             h.sendMessage(m);  
  99.         }  
  100.     }  
  101. }  
  102. // ------------------------------------------------------  
posted on 2011-04-15 10:08  kitea  阅读(133)  评论(0)    收藏  举报