天天看點

Android 的消息機制(1)

在Android主線程啟動時,就會執行Looper對象的消息圈(Message Loop)去監視該線程中的消息隊列(Message Queue),當Message Queue中有消息,主線程就會取出此消息,然後處理之。注意:此Looper對象和消息隊列對象都是此線程專屬的,各隻有一個,自己線程的Looper隻監視自己線程的MQ,而Handler對象可以有多個。

但是我們自己生成的子線程并不會自動建立Looper對象,但是可以建立Looper對象以及一個Message Queue資料結構。

<a href="http://blog.51cto.com/attachment/201111/112431765.png" target="_blank"></a>

 代碼:

* 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();  

        }  

    }  

//------------------------------------------------------  

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!"到畫面上。

本文轉自 最牛傻蛋 51CTO部落格,原文連結:http://blog.51cto.com/zuiniuwang/718341,如需轉載請自行聯系原作者

繼續閱讀