主线程发送消息到工作线程,工作线程的步骤是固定为3步的。
1 Looper.prepare();//步骤1,线程里使用handler必须这样写,2 handler = new Handler(){ //步骤2,先实例化handler3 @Override4 public void handleMessage(Message msg) {5 String receiver = msg.obj.toString();6 System.out.println("receiver:"+receiver+","+Thread.currentThread().getName());7 }8 };9 Looper.loop();//步骤3 ,循环Looper
代码如下:
1 package com.wyl.handler_mars; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.os.Handler; 6 import android.os.Looper; 7 import android.os.Message; 8 import android.util.Log; 9 import android.view.View;10 import android.view.View.OnClickListener;11 import android.widget.Button;12 import android.widget.TextView;13 import android.widget.Toast;14 /**15 * 主线程发送消息,工作线程接收消息16 * @author Wyl17 *18 */19 public class SecondActivity extends Activity implements OnClickListener{20 Button btn_fasong;21 Handler hand;22 Message msg;23 TextView tv;24 @Override25 protected void onCreate(Bundle savedInstanceState) {26 // TODO Auto-generated method stub27 super.onCreate(savedInstanceState);28 setContentView(R.layout.main2);29 btn_fasong = (Button) findViewById(R.id.btn_send);30 tv = (TextView) findViewById(R.id.tv_01);31 btn_fasong.setOnClickListener(this);32 Thread t = new MyThread();33 t.start();34 }35 @Override36 public void onClick(View v) {37 //hand = new Handler();//这一行尤其重要,如果不注释掉,那么下面的线程中就没法接收到消息,因为这行代码中又重新生成了一个handler对象,也就对应着不同的looper,所以就没法38 Message msg = hand.obtainMessage();39 msg.obj = "无语了。。。";40 System.out.println("onClick()...发送:"+"无语了。。。");41 hand.sendMessage(msg);//主线程发送消息42 }43 44 45 class MyThread extends Thread{46 47 @Override48 public void run() {49 Looper.prepare();//获取一个Looper对象50 System.out.println("===========");51 hand = new Handler(){52 53 @Override54 public void handleMessage(Message msg) {55 // TODO Auto-generated method stub56 System.out.println("MyThread.run()接收到了消息"+",所在线程:"+Thread.currentThread().getName());57 }58 };59 Looper.loop();60 }61 62 }63 }
===============分割线===============
20151008重新总结,具体的说明在注释里有详细的解释,
1 26 7 13 18 19 20
MainActivity.java
1 package com.wyl.handler; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.os.Handler; 6 import android.os.Looper; 7 import android.os.Message; 8 import android.view.View; 9 import android.view.View.OnClickListener;10 import android.widget.Button;11 12 public class MainActivity extends Activity{13 Button btn_fs;//发送14 Button btn_qx;//取消15 Handler handler;16 @Override17 protected void onCreate(Bundle savedInstanceState) {18 super.onCreate(savedInstanceState);19 setContentView(R.layout.main);20 btn_fs = (Button) findViewById(R.id.btn_send);21 btn_qx = (Button) findViewById(R.id.btn_Cancel);22 btn_fs.setOnClickListener(new OnClickListener() {23 @Override24 public void onClick(View v) {25 Message msg = handler.obtainMessage();26 String str = "我是wyl";27 msg.obj = str;28 System.out.println("onClick()发送:"+str+","+Thread.currentThread().getName());29 handler.sendMessage(msg);30 }31 });32 /*33 * 这个线程里的handler在onClick()方法之前使用到,所以需要在线程的run()方法里34 * 实例化,onClick()方法里就不用实例化了,也不能怪进行重新实例化,否则又是实例化的35 * handler对象就不是一个了,handler不一致的话就会导致循环的队列不是同一个,就会36 * 导致workThread里的handleMessage()方法没法接收到UIThread发出的消息37 */38 MyThread t = new MyThread();39 t.start();40 41 }42 43 class MyThread extends Thread{44 @Override45 public void run() {46 Looper.prepare();//步骤1,线程里使用handler必须这样写,47 handler = new Handler(){ //步骤2,先实例化handler48 @Override49 public void handleMessage(Message msg) {50 String receiver = msg.obj.toString();51 System.out.println("receiver:"+receiver+","+Thread.currentThread().getName());52 }53 };54 Looper.loop();//步骤3 ,循环Looper55 }56 }57 58 }