Android 的消息机制(4)

上面的文章我们演示了如何把一个Message由子线程发送给主线程,但是如何将一个Message从主线程发送给子线程呢?子线程在默认的情况下是没有Looper的,也就没有可能操作子线程的消息队列。我们通过查API文档可以看到:

Class used to run a message loop for a thread. Threads by default do not have a message loop associated with them; to create one, call prepare() in the thread that is to run the loop, and then loop() to have it process messages until the loop is stopped.

Most interaction with a message loop is through the Handler class.

大概意思是,线程在默认情况下是没有Looper的,但是可以通过调用Prepare()方法来运行一个loop,然后使用loop()方法来处理消息直到loop结束。通常的使用方式是:

?[Copy to clipboard] Download zuiniuwang.java
 
 
 
 
  1. class LooperThread extends Thread {  
  2.     public Handler mHandler;  
  3.    
  4.     public void run() {  
  5.         Looper.prepare();  
  6.         mHandler = new Handler() {  
  7.             public void handleMessage(Message msg) { // process incoming  
  8.                                                         // messages here  
  9.             }  
  10.         };  
  11.         Looper.loop();  
  12.     }  
  13.    

例程如下,基本过程是这样的,启动主线程,然后启动子线程,子线程注册Looper,主线程发送一个Message给子线程,然后子线程的handler处理此消息,再把此消息发送给主线程的消息队列,主线程空间显示此消息~

?[Copy to clipboard] Download zuiniuwang.java
 
 
 
 
  1. /**  
  2.  * MessageExample4.java  
  3.  * com.test.Message  
  4.  *  
  5.  * Function: TODO  
  6.  *  
  7.  *   ver     date           author  
  8.  * ──────────────────────────────────  
  9.  *           2011-3-23      Leon  
  10.  *  
  11.  * Copyright (c) 2011, TNT All Rights Reserved.  
  12.  */  
  13.    
  14. package com.test.Message;  
  15.    
  16. import android.app.Activity;  
  17. import android.graphics.Color;  
  18. import android.os.Bundle;  
  19. import android.os.Handler;  
  20. import android.os.Looper;  
  21. import android.os.Message;  
  22. import android.view.View;  
  23. import android.view.View.OnClickListener;  
  24. import android.widget.Button;  
  25. import android.widget.LinearLayout;  
  26. import android.widget.TextView;  
  27.    
  28. /**  
  29.  * ClassName:MessageExample4 主线程如何传递消息给子线程 Function: TODO ADD FUNCTION Reason:  
  30.  * TODO ADD REASON  
  31.  *  
  32.  * @author Leon  
  33.  * @version  
  34.  * @since Ver 1.1  
  35.  * @Date 2011-3-23  
  36.  */  
  37. public class MessageExample4 extends Activity implements OnClickListener {  
  38.    
  39.     private final int WC = LinearLayout.LayoutParams.WRAP_CONTENT;  
  40.     private final int FP = LinearLayout.LayoutParams.FILL_PARENT;  
  41.     public TextView tv;  
  42.     private Button btn, btn2;  
  43.     private Handler h;  
  44.    
  45.     public void onCreate(Bundle icicle) {  
  46.         super.onCreate(icicle);  
  47.         LinearLayout layout = new LinearLayout(this);  
  48.         layout.setOrientation(LinearLayout.VERTICAL);  
  49.    
  50.         btn = new Button(this);  
  51.         btn.setId(101);  
  52.    
  53.         btn.setText("test looper");  
  54.         btn.setOnClickListener(this);  
  55.         LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(100, 50);  
  56.         param.topMargin = 10;  
  57.         layout.addView(btn, param);  
  58.    
  59.         btn2 = new Button(this);  
  60.         btn2.setId(102);  
  61.    
  62.         btn2.setText("exit");  
  63.         btn2.setOnClickListener(this);  
  64.         layout.addView(btn2, param);  
  65.    
  66.         tv = new TextView(this);  
  67.         tv.setTextColor(Color.WHITE);  
  68.         tv.setText("");  
  69.         LinearLayout.LayoutParams param2 = new LinearLayout.LayoutParams(FP, WC);  
  70.         param2.topMargin = 10;  
  71.         layout.addView(tv, param2);  
  72.    
  73.         setContentView(layout);  
  74.         // 启动子线程  
  75.         new myThread().start();  
  76.     }  
  77.    
  78.     public void onClick(View v) {  
  79.         switch (v.getId()) {  
  80.         case 101:  
  81.             String obj = "mainThread";  
  82.             Message m = h.obtainMessage(1, 1, 1, obj);  
  83.             h.sendMessage(m);  
  84.             break;  
  85.         case 102:  
  86.             finish();  
  87.             break;  
  88.         }  
  89.    
  90.     }  
  91.    
  92.     // ------------------------------------------------------  
  93.    
  94.     class EHandler extends Handler {  
  95.         public EHandler(Looper looper) {  
  96.             super(looper);  
  97.         }  
  98.    
  99.         @Override  
  100.         public void handleMessage(Message msg) {  
  101.             tv.setText((String) msg.obj);  
  102.         }  
  103.     }  
  104.    
  105.     // ------------------------------------------------------  
  106.     class myThread extends Thread {  
  107.         private EHandler mHandler;  
  108.    
  109.         public void run() {  
  110.             Looper.prepare();  
  111.             h = new Handler() {  
  112.                 public void handleMessage(Message msg) {  
  113.                     EHandler ha = new EHandler(Looper.getMainLooper());  
  114.                     String obj = (String) msg.obj + ", myThread";  
  115.                     Message m = ha.obtainMessage(1, 1, 1, obj);  
  116.                     ha.sendMessage(m);  
  117.                 }  
  118.             };  
  119.             Looper.loop();  
  120.    
  121.         }  
  122.     }  
  123.    

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值