上一节中,是主线程自己发了一个消息到自己的Message Queue中,并把消息从队列中提取出来。那么如何由别的线程发送消息给主线程的Message Queue中呢?
直接看代码~~?[Copy to clipboard]Download zuiniuwang.java
- <span style="font-size: 14px">/**
- * MessageQueue2.java
- * com.test
- *
- * Function: TODO
- *
- * ver date author
- * ──────────────────────────────────
- * 2011-3-20 Leon
- *
- * Copyright (c) 2011, TNT All Rights Reserved.
- */
- package com.test;
- /**
- * ClassName:MessageQueue2
- * Function: TODO ADD FUNCTION
- * Reason: TODO ADD REASON
- *
- * @author Leon
- * @version
- * @since Ver 1.1
- * @Date 2011-3-20
- */
- 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 MessageQueue2 extends Activity implements OnClickListener {
- private final int WC = LinearLayout.LayoutParams.WRAP_CONTENT;
- private final int FP = LinearLayout.LayoutParams.FILL_PARENT;
- public TextView tv;
- private myThread t;
- 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:
- t = new myThread();
- t.start();
- break;
- case 102:
- finish();
- break;
- }
- }
- //------------------------------------------------------
- class EHandler extends Handler {
- public EHandler(Looper looper) {
- super(looper);
- }
- @Override
- public void handleMessage(Message msg) {
- tv.setText((String)msg.obj);
- }
- }
- //------------------------------------------------------
- class myThread extends Thread{
- private EHandler mHandler;
- public void run() {
- Looper myLooper, mainLooper;
- //取出本线程中的Looper,当然如果本句话放在主线程中,则会取得主线程的Looper
- myLooper = Looper.myLooper();
- //取出主线程中的Looper
- mainLooper = Looper.getMainLooper();
- String obj;
- if(myLooper == null){
- mHandler = new EHandler(mainLooper);
- obj = "current thread has no looper,this message is from Main thread!";
- }
- else {
- mHandler = new EHandler(myLooper);
- obj = "This is from new thread.";
- }
- mHandler.removeMessages(0);
- Message m = mHandler.obtainMessage(1, 1, 1, obj);
- mHandler.sendMessage(m);
- }
- }
- }
在本程序中Android仍然会自动替主线程建立Message Queue。在点击按钮之后,新生成的子线程中并不会建立Message Queue。所以,新线程中的myLooper值为null,而mainLooper则指向主线程里的Looper对象。于是,执行当到指令:
mHandler = new EHandler(mainLooper); 时这个mHandler是属于主线程的。
通过指令:mHandler.sendMessage(m); 便将消息m存入到主线程的Message Queue里。mainLooper看到Message Queue里有讯息,就会自动处理,主线程会自动回调mHandler的handleMessage()函数来处理消息。
本文转自 最牛傻蛋 51CTO博客,原文链接:http://blog.51cto.com/zuiniuwang/718340,如需转载请自行联系原作者