这个例子也是Demo4的另外一种实现方式,大家可以对比学习: 
Java代码  
  1. package com.example.Looper_05;  
  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_05 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 RR r;  
  21.     private Button btn, btn2;  
  22.     private Handler h;  
  23.     private 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.         r = new RR();  
  53.     }  
  54.   
  55.     public void onClick(View v) {  
  56.         switch (v.getId()) {  
  57.         case 101:  
  58.             String obj = "mainThread";  
  59.             Message m = h.obtainMessage(111, obj);  
  60.             h.sendMessage(m);  
  61.             break;  
  62.         case 102:  
  63.             h.getLooper().quit();  
  64.             finish();  
  65.             break;  
  66.         }  
  67.     }  
  68.   
  69.     // ------------------------------------------------  
  70.     public class EventHandler extends Handler {  
  71.         public EventHandler(Looper looper) {  
  72.             super(looper);  
  73.         }  
  74.   
  75.         @Override  
  76.         public void handleMessage(Message msg) {  
  77.             ((Activity) ctx).setTitle((String) msg.obj);  
  78.         }  
  79.     }  
  80.   
  81.     // ------------------------------------------------  
  82.     public class RR implements Runnable {  
  83.         public RR() {  
  84.             Thread aThread = new Thread(nullthis"RR");  
  85.             aThread.start();  
  86.         }  
  87.   
  88.         public void run() {  
  89.             Looper.prepare();  
  90.             h = new Handler() {  
  91.                 public void handleMessage(Message msg) {  
  92.                     EventHandler ha = new EventHandler(Looper.getMainLooper());  
  93.                     String obj = (String) msg.obj + ", myThread";  
  94.                     Message m = ha.obtainMessage(111, obj);  
  95.                     ha.sendMessage(m);  
  96.                 }  
  97.             };  
  98.             Looper.loop();  
  99.         }  
  100.     }     
  101. }  
posted on 2011-04-15 10:16  kitea  阅读(120)  评论(0)    收藏  举报