代码改变世界

Android的消息机制(3)

2011-12-30 11:46  tang768168  阅读(165)  评论(0编辑  收藏  举报

当然我们也可以隐式的指定Looper ,代码如下:

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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
 * MessageQueue2.java
 * com.test
 *
 * Function: TODO
 *
 *   ver     date      		author
 * ──────────────────────────────────
 *   		 2011-3-20 		Leon
 *
 * Copyright (c) 2011, TNT All Rights Reserved.
*/
 
package com.test.messagequeue;
/**
 * 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 MessageQueue3 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;
    private EHandler mHandler;
 
    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:
//			 mHandler = new EHandler(Looper.myLooper());
			 //可以隐式的制定Looper,和上面的效果是一样的
			 mHandler = new EHandler();
			 t = new myThread();
		     t.start();
		     break;
		case 102:
	    	finish();
			break;
		}
	}
//------------------------------------------------------
class EHandler extends Handler {
	        public EHandler(Looper looper) {
	            super(looper);
	        }
	        public EHandler(){
 
	        }
	        @Override
	        public void handleMessage(Message msg) {
	           tv.setText((String)msg.obj);
	    }
	}
//------------------------------------------------------
class myThread extends Thread{
 
	 public void run() {
 
	     String obj = "This message is from new thread.";
	     mHandler.removeMessages(0);
	     Message m = mHandler.obtainMessage(1, 1, 1, obj);
	     mHandler.sendMessage(m);
	  }
  }
}

在此代码中,指令:h = new EventHandler();就等于:h = new EventHandler(Looper.myLooper());
它建立了当前线程(Current Thread)的EventHandler对象。由于是由main线程执行此指令的,所以此EventHandler对象是用来存取main线程的Message Queue的。

 本文章及代码由傻蛋整理及测试,转载请注明

转载自最牛网

本文链接地址: Android的消息机制(3)