http://www.open-open.com/lib/view/open1331276072249.html
一、几个关键概念
1、主线程给自己发送Message
package test.message; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.view.View; import android.widget.Button; import android.widget.TextView; public classMainActivity extends Activity { privateButton btnTest; privateTextView textView; privateHandler handler; @Override publicvoid onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btnTest = (Button)this.findViewById(R.id.btn_01); textView = (TextView)this.findViewById(R.id.view_01); btnTest.setOnClickListener(newView.OnClickListener() { @Override publicvoid onClick(View arg0) { Looper looper = Looper.getMainLooper();//主线程的Looper对象 //这里以主线程的Looper对象创建了handler, //所以,这个handler发送的Message会被传递给主线程的MessageQueue。 handler =new MyHandler(looper); handler.removeMessages(0); //构建Message对象 //第一个参数:是自己指定的message代号,方便在handler选择性地接收 //第二三个参数没有什么意义 //第四个参数需要封装的对象 Message msg = handler.obtainMessage(1,1,1,"主线程发消息了"); handler.sendMessage(msg);//发送消息 } }); } class MyHandler extends Handler{ publicMyHandler(Looper looper){ super(looper); } publicvoid handleMessage(Message msg){ super.handleMessage(msg); textView.setText("我是主线程的Handler,收到了消息:"+(String)msg.obj); } } }
|
2、其他线程给主线程发送Message
package test.message; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.view.View; import android.widget.Button; import android.widget.TextView; public classMainActivity extends Activity { privateButton btnTest; privateTextView textView; privateHandler handler; @Override publicvoid onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btnTest = (Button)this.findViewById(R.id.btn_01); textView = (TextView)this.findViewById(R.id.view_01); btnTest.setOnClickListener(newView.OnClickListener() { @Override publicvoid onClick(View arg0) { //可以看出这里启动了一个线程来操作消息的封装和发送的工作 //这样原来主线程的发送就变成了其他线程的发送,简单吧?呵呵 newMyThread().start(); } }); } classMyHandler extends Handler{ publicMyHandler(Looper looper){ super(looper); } publicvoid handleMessage(Message msg){ super.handleMessage(msg); textView.setText("我是主线程的Handler,收到了消息:"+(String)msg.obj); } } //加了一个线程类 classMyThread extends Thread{ publicvoid run(){ Looper looper = Looper.getMainLooper();//主线程的Looper对象 //这里以主线程的Looper对象创建了handler, //所以,这个handler发送的Message会被传递给主线程的MessageQueue。 handler =new MyHandler(looper); //构建Message对象 //第一个参数:是自己指定的message代号,方便在handler选择性地接收 //第二三个参数没有什么意义 //第四个参数需要封装的对象 Message msg = handler.obtainMessage(1,1,1,"其他线程发消息了"); handler.sendMessage(msg);//发送消息 } } } <span style="font-family:Arial;"> <span style="white-space:normal;"> </span> </span> </b>
3、主线程给其他线程发送Message
01 <b>packagetest.message; 02 03 import android.app.Activity; 04 import android.os.Bundle; 05 import android.os.Handler; 06 import android.os.Looper; 07 import android.os.Message; 08 import android.view.View; 09 import android.widget.Button; 10 import android.widget.TextView; 11 12 public classMainActivity extends Activity { 13 14 privateButton btnTest; 15 privateTextView textView; 16 17 privateHandler handler; 18 19 @Override 20 publicvoid onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState); 22 setContentView(R.layout.main); 23 24 btnTest = (Button)this.findViewById(R.id.btn_01); 25 textView = (TextView)this.findViewById(R.id.view_01); 26 27 28 //启动线程 29 newMyThread().start(); 30 31 btnTest.setOnClickListener(newView.OnClickListener() { 32 33 @Override 34 publicvoid onClick(View arg0) { 35 //这里handler的实例化在线程中 36 //线程启动的时候就已经实例化了 37 Message msg = handler.obtainMessage(1,1,1,"主线程发送的消息"); 38 handler.sendMessage(msg); 39 } 40 }); 41 } 42 43 classMyHandler extends Handler{ 44 45 publicMyHandler(Looper looper){ 46 super(looper); 47 } 48 49 publicvoid handleMessage(Message msg){ 50 super.handleMessage(msg); 51 textView.setText("我是主线程的Handler,收到了消息:"+(String)msg.obj); 52 } 53 } 54 55 classMyThread extends Thread{ 56 57 publicvoid run(){ 58 Looper.prepare();//创建该线程的Looper对象,用于接收消息 59 60 //注意了:这里的handler是定义在主线程中的哦,呵呵, 61 //前面看到直接使用了handler对象,是不是在找,在什么地方实例化的呢? 62 //现在看到了吧???呵呵,开始的时候实例化不了,因为该线程的Looper对象 63 //还不存在呢。现在可以实例化了 64 //这里Looper.myLooper()获得的就是该线程的Looper对象了 65 handler =new ThreadHandler(Looper.myLooper()); 66 67 //这个方法,有疑惑吗? 68 //其实就是一个循环,循环从MessageQueue中取消息。 69 //不经常去看看,你怎么知道你有新消息呢??? 70 Looper.loop(); 71 72 } 73 74 //定义线程类中的消息处理类 75 classThreadHandler extends Handler{ 76 77 publicThreadHandler(Looper looper){ 78 super(looper); 79 } 80 81 publicvoid handleMessage(Message msg){ 82 //这里对该线程中的MessageQueue中的Message进行处理 83 //这里我们再返回给主线程一个消息 84 handler =new MyHandler(Looper.getMainLooper()); 85 86 Message msg2 = handler.obtainMessage(1,1,1,"子线程收到:"+(String)msg.obj); 87 88 handler.sendMessage(msg2); 89 } 90 } 91 } 92 }</b>
|
4、其他线程给自己发送Message
01 <b>packagetest.message; 02 03 import android.app.Activity; 04 import android.os.Bundle; 05 import android.os.Handler; 06 import android.os.Looper; 07 import android.os.Message; 08 import android.view.View; 09 import android.widget.Button; 10 import android.widget.TextView; 11 12 public classMainActivity extends Activity { 13 14 privateButton btnTest; 15 privateTextView textView; 16 17 privateHandler handler; 18 19 @Override 20 publicvoid onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState); 22 setContentView(R.layout.main); 23 24 btnTest = (Button)this.findViewById(R.id.btn_01); 25 textView = (TextView)this.findViewById(R.id.view_01); 26 27 28 btnTest.setOnClickListener(newView.OnClickListener() { 29 30 @Override 31 publicvoid onClick(View arg0) { 32 //启动线程 33 newMyThread().start(); 34 } 35 }); 36 } 37 38 classMyHandler extends Handler{ 39 40 publicMyHandler(Looper looper){ 41 super(looper); 42 } 43 44 publicvoid handleMessage(Message msg){ 45 super.handleMessage(msg); 46 textView.setText((String)msg.obj); 47 } 48 } 49 50 classMyThread extends Thread{ 51 52 publicvoid run(){ 53 Looper.prepare();//创建该线程的Looper对象 54 //这里Looper.myLooper()获得的就是该线程的Looper对象了 55 handler =new ThreadHandler(Looper.myLooper()); 56 Message msg = handler.obtainMessage(1,1,1,"我自己"); 57 handler.sendMessage(msg); 58 59 Looper.loop(); 60 61 } 62 63 //定义线程类中的消息处理类 64 classThreadHandler extends Handler{ 65 66 publicThreadHandler(Looper looper){ 67 super(looper); 68 } 69 70 publicvoid handleMessage(Message msg){ 71 //这里对该线程中的MessageQueue中的Message进行处理 72 //这里我们再返回给主线程一个消息 73 //加入判断看看是不是该线程自己发的信息 74 if(msg.what ==1 && msg.obj.equals("我自己")){ 75 76 handler =new MyHandler(Looper.getMainLooper()); 77 78 Message msg2 = handler.obtainMessage(1,1,1,"禀告主线程:我收到了自己发给自己的Message"); 79 80 handler.sendMessage(msg2); 81 } 82 83 } 84 } 85 } 86 }</b>