这个例子主要演示了主,子线程之间的信息交互 
Java代码  
  1. package com.example.Looper_04;  
  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_04 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.     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.         t = new myThread();  
  53.         t.start();  
  54.     }  
  55.   
  56.     public void onClick(View v) {  
  57.         switch (v.getId()) {  
  58.         case 101:  
  59.             String obj = "mainThread";  
  60.             Message m = h.obtainMessage(111, obj);  
  61.             // 发送的消息由子线程的handler来进行处理  
  62.             h.sendMessage(m);  
  63.             break;  
  64.         case 102:  
  65.             h.getLooper().quit();  
  66.             finish();  
  67.             break;  
  68.         }  
  69.     }  
  70.   
  71.     // ------------------------------------------------  
  72.     public class EventHandler extends Handler {  
  73.         public EventHandler(Looper looper) {  
  74.             super(looper);  
  75.         }  
  76.   
  77.         @Override  
  78.         public void handleMessage(Message msg) {  
  79.             ((Activity) ctx).setTitle((String) msg.obj);  
  80.         }  
  81.     }  
  82.   
  83.     // ------------------------------------------------  
  84.     class myThread extends Thread {  
  85.         public void run() {  
  86.             // 初始化当前子线程的Looper物件之管理对象  
  87.             Looper.prepare();  
  88.             h = new Handler() {  
  89.                 public void handleMessage(Message msg) {  
  90.                     //又建立了主线程的handler对象  
  91.                     EventHandler ha = new EventHandler(Looper.getMainLooper());  
  92.                     String obj = (String) msg.obj + ", myThread";  
  93.                     Message m = ha.obtainMessage(111, obj);  
  94.                     ha.sendMessage(m);  
  95.                 }  
  96.             };  
  97.             Looper.loop();  
  98.         }  
  99.     }  
  100. }  
posted on 2011-04-15 10:15  kitea  阅读(117)  评论(0)    收藏  举报