Android的消息机制(1)
2011-12-30 11:31 tang768168 阅读(216) 评论(0) 编辑 收藏 举报在Android主线程启动时,就会执行Looper对象的消息圈(Message Loop)去监视该线程中的消息队列(Message Queue),当Message Queue中有消息,主线程就会取出此消息,然后处理之。注意:此Looper对象和消息队列对象都是此线程专属的,各只有一个,自己线程的Looper 只监视自己线程的MQ,而Handler对象可以有多个。
但是我们自己生成的子线程并不会自动建立Looper对象,但是可以创建Looper对象以及一个Message Queue数据结构。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
/** * MessageQueue.java * com.test * * Function: TODO * * ver date author * ────────────────────────────────── * 2011-3-19 Leon * * Copyright (c) 2011, TNT All Rights Reserved. */ package com.test; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; public class MessageQueue extends Activity implements OnClickListener { private final int WC = LinearLayout.LayoutParams.WRAP_CONTENT; private final int FP = LinearLayout.LayoutParams.FILL_PARENT; public TextView tv; private EventHandler mHandler; private Button btn, btn2, btn3; public void onCreate(Bundle icicle) { super.onCreate(icicle); LinearLayout layout = new LinearLayout(this); layout.setOrientation(LinearLayout.VERTICAL); btn = new Button(this); btn.setId(101); btn.setText("test looper"); btn.setOnClickListener(this); LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(100,50); param.topMargin = 10; layout.addView(btn, param); btn2 = new Button(this); btn2.setId(102); btn2.setText("exit"); btn2.setOnClickListener(this); layout.addView(btn2, param); tv = new TextView(this); tv.setTextColor(Color.WHITE); tv.setText(""); LinearLayout.LayoutParams param2 = new LinearLayout.LayoutParams(FP, WC); param2.topMargin = 10; layout.addView(tv, param2); setContentView(layout); } public void onClick(View v) { switch(v.getId()){ case 101: Looper looper; looper = Looper.myLooper(); mHandler = new EventHandler(looper); // 清除整个MessageQueue里的事件,确保不会通知到别人 mHandler.removeMessages(0); String obj = "This my message!"; // 组装成一个Message对象 Message m = mHandler.obtainMessage(1, 1, 1, obj); // 将Message物件送入MessageQueue里 mHandler.sendMessage(m); break; case 102: finish(); break; } } //------------------------------------------------------ class EventHandler extends Handler { public EventHandler(Looper looper) { super(looper); } @Override public void handleMessage(Message msg) { tv.setText((String)msg.obj); } } } |
说明:
在此程序启动时,当前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!"到画面上。
转载自最牛网
本文链接地址: Android的消息机制(1)