Android 的消息机制(1)

在Android主线程启动时,就会执行Looper对象的消息圈(Message Loop)去监视该线程中的消息队列(Message Queue),当Message Queue中有消息,主线程就会取出此消息,然后处理之。注意:此Looper对象和消息队列对象都是此线程专属的,各只有一个,自己线程的Looper只监视自己线程的MQ,而Handler对象可以有多个。

但是我们自己生成的子线程并不会自动建立Looper对象,但是可以创建Looper对象以及一个Message Queue数据结构。

 

 

 代码:

?[Copy to clipboard] Download zuiniuwang.java
 
  
  1. * MessageQueue.java  
  2.  * com.test  
  3.  *  
  4.  * Function: TODO  
  5.  *  
  6.  *   ver     date           author  
  7.  * ──────────────────────────────────  
  8.  *           2011-3-19      Leon  
  9.  *  
  10.  * Copyright (c) 2011, TNT All Rights Reserved.  
  11. */  
  12. package com.test;  
  13.    
  14. import android.app.Activity;  
  15. import android.graphics.Color;  
  16. import android.os.Bundle;  
  17. import android.os.Handler;  
  18. import android.os.Looper;  
  19. import android.os.Message;  
  20. import android.view.View;  
  21. import android.view.View.OnClickListener;  
  22. import android.widget.Button;  
  23. import android.widget.LinearLayout;  
  24. import android.widget.TextView;  
  25.    
  26. public class MessageQueue extends Activity implements OnClickListener {  
  27.     private final int WC = LinearLayout.LayoutParams.WRAP_CONTENT;  
  28.     private final int FP = LinearLayout.LayoutParams.FILL_PARENT;  
  29.     public TextView tv;  
  30.     private EventHandler mHandler;  
  31.     private Button btn, btn2, btn3;  
  32.    
  33.     public void onCreate(Bundle icicle) {  
  34.                 super.onCreate(icicle);  
  35.                 LinearLayout layout = new LinearLayout(this);  
  36.                 layout.setOrientation(LinearLayout.VERTICAL);  
  37.    
  38.                 btn = new Button(this);  
  39.                 btn.setId(101);  
  40.    
  41.                 btn.setText("test looper");  
  42.                 btn.setOnClickListener(this);  
  43.                 LinearLayout.LayoutParams param =  
  44.                     new LinearLayout.LayoutParams(100,50);  
  45.                 param.topMargin = 10;  
  46.                 layout.addView(btn, param);   
  47.    
  48.                 btn2 = new Button(this);  
  49.                 btn2.setId(102);  
  50.    
  51.                 btn2.setText("exit");  
  52.                 btn2.setOnClickListener(this);  
  53.                 layout.addView(btn2, param);  
  54.    
  55.                 tv = new TextView(this);  
  56.                 tv.setTextColor(Color.WHITE);  
  57.                 tv.setText("");  
  58.                 LinearLayout.LayoutParams param2 =  
  59.                    new LinearLayout.LayoutParams(FP, WC);  
  60.                 param2.topMargin = 10;  
  61.                 layout.addView(tv, param2);  
  62.                 setContentView(layout);  
  63.                }  
  64.         public void onClick(View v) {  
  65.         switch(v.getId()){  
  66.         case 101:  
  67.             Looper looper;  
  68.              looper = Looper.myLooper();  
  69.              mHandler = new EventHandler(looper);  
  70.              // 清除整个MessageQueue里的事件,确保不会通知到别人  
  71.              mHandler.removeMessages(0);  
  72.    
  73.              String obj = "This my message!";  
  74.              // 组装成一个Message对象  
  75.              Message m = mHandler.obtainMessage(1, 1, 1, obj);  
  76.              // 将Message物件送入MessageQueue里  
  77.              mHandler.sendMessage(m);  
  78.             break;  
  79.         case 102:  
  80.             finish();  
  81.             break;  
  82.         }  
  83.     }  
  84. //------------------------------------------------------  
  85. class EventHandler extends Handler  
  86.         {  
  87.             public EventHandler(Looper looper) {  
  88.                 super(looper);  
  89.             }  
  90.             @Override  
  91.             public void handleMessage(Message msg) {  
  92.                tv.setText((String)msg.obj);  
  93.         }  
  94.     }  
 
 

说明:

在此程序启动时,当前Activity线程(即主线程, main thread)会自动产生一个Looper对象,并且有了一个MessageQueue数据结构。

当按钮点击后,指令:looper = Looper.myLooper(); 就呼叫Looper类别的静态myLooper()函数,以取得目前线程里的Looper对象的引用。

指令:mHandler = new EventHandler(looper);

用于生成一个Handler的子类EventHandler对象,同时指明是和哪个Looper沟通。Activity等对象可以通过EventHandler对象来将消息传放入MessageQueue里,EventHandler对象也扮演消息Listener的角色,可接收Looper对象所送来的讯息。如下图:

指令:Message m = mHandler.obtainMessage(1, 1, 1, obj);

先诞生一个Message对象,并将数据存入这个对象中。指令:mHandler.sendMessage(m);通过mHandler对象而将讯息放入MessageQueue里。此时,Looper对象看到MessageQueue里有讯息m,就将它广播出去,mHandler对象接到此消息时,会呼叫其handleMessage()函数来处理之,于是输出"This my message!"到画面上。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值