---
title: "Android消息处理机制(二)"
author: "xtfncel"
date: "2010-05-11T06:07:06Z"
tags: []
categories: ["移动开发"]
slug: "android"
draft: false
lastmod: "2010-05-11T06:07:06Z"
id: 663518
url: "https://xtfncel.iteye.com/blog/663518"
---

Android消息处理机制(二)

角色综述（回顾）:

   (1)UI thread 通常就是main thread，而Android启动程序时会替它建立一个MessageQueue。

(2)当然需要一个Looper对象，来管理该MessageQueue。

(3)我们可以构造Handler对象来push新消息到Message Queue里；或者接收Looper(从Message Queue取出)所送来的消息。

(4)线程A的Handler对象可以传递给别的线程，让别的线程B或C等能送讯息来给线程A(存于A的Message Queue里)。

(5)线程A的Message Queue里的消息，只有线程A所属的对象可以处理。

 

子线程传递消息给主线程

**public** **class** Activity2 **extends** Activity **implements** OnClickListener{

       Button button = **null**;

       TextView text = **null**;

       MyHandler mHandler = **null**;

       Thread thread ;

       @Override

       **protected** **void** onCreate(Bundle savedInstanceState) {

              **super**.onCreate(savedInstanceState);

              setContentView(R.layout.*activity1*);        

              button = (Button)findViewById(R.id.*btn*);

              button.setOnClickListener(**this**);

              text = (TextView)findViewById(R.id.*content*);

       }

       **public** **void** onClick(View v) {

              **switch** (v.getId()) {

              **case** R.id.*btn*:

                     thread = **new** MyThread();

                     thread.start();

                     **break**;

              }            

       }     

       **private** **class** MyHandler **extends** Handler{             

              **public** MyHandler(Looper looper){

                     **super**(looper);

              }

              @Override

              **public** **void** handleMessage(Message msg) {//处理消息

                     text.setText(msg.obj.toString());

              }            

       }

       **private** **class** MyThread **extends** Thread{

              @Override

              **public** **void** run() {

                     Looper curLooper = Looper.*myLooper*();

                     Looper mainLooper = Looper.*getMainLooper*();

                     String msg ;

                     **if**(curLooper==**null**){

                            mHandler = **new** MyHandler(mainLooper);

                            msg = "curLooper is null";

                     }**else**{

                            mHandler = **new** MyHandler(curLooper);

                            msg = "This is curLooper";

                     }

                     mHandler.removeMessages(0);

                     Message m = mHandler.obtainMessage(1, 1, 1, msg);

                     mHandler.sendMessage(m);

              }            

       }

}

说明：

Android会自动替主线程建立Message Queue。在这个子线程里并没有建立Message Queue。所以，myLooper值为null，而mainLooper则指向主线程里的Looper。于是，执行到：

mHandler = new MyHandler (mainLooper);

此mHandler属于主线程。

   mHandler.sendMessage(m);

就将m消息存入到主线程的Message Queue里。mainLooper看到Message Queue里有讯息，就会作出处理，于是由主线程执行到mHandler的handleMessage()来处理消息。

下一节将会写一个关于应多线程请求网络数据的例子。