主线程发送消息到工作线程,工作线程的步骤是固定为3步的。
1 Looper.prepare();//步骤1,线程里使用handler必须这样写, 2 handler = new Handler(){//步骤2,先实例化handler 3 @Override 4 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 Wyl 17 * 18 */ 19 public class SecondActivity extends Activity implements OnClickListener{ 20 Button btn_fasong; 21 Handler hand; 22 Message msg; 23 TextView tv; 24 @Override 25 protected void onCreate(Bundle savedInstanceState) { 26 // TODO Auto-generated method stub 27 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 @Override 36 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 @Override 48 public void run() { 49 Looper.prepare();//获取一个Looper对象 50 System.out.println("==========="); 51 hand = new Handler(){ 52 53 @Override 54 public void handleMessage(Message msg) { 55 // TODO Auto-generated method stub 56 System.out.println("MyThread.run()接收到了消息"+",所在线程:"+Thread.currentThread().getName()); 57 } 58 }; 59 Looper.loop(); 60 } 61 62 } 63 }
===============分割线===============
20151008重新总结,具体的说明在注释里有详细的解释,
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" > 6 7 <Button 8 android:id="@+id/btn_send" 9 android:onClick="doClick" 10 android:layout_width="match_parent" 11 android:layout_height="wrap_content" 12 android:hint="发送" /> 13 <Button 14 android:id="@+id/btn_Cancel" 15 android:layout_width="match_parent" 16 android:layout_height="wrap_content" 17 android:hint="取消发送" /> 18 19 20 </LinearLayout>
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 @Override 17 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 @Override 24 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 @Override 45 public void run() { 46 Looper.prepare();//步骤1,线程里使用handler必须这样写, 47 handler = new Handler(){//步骤2,先实例化handler 48 @Override 49 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 ,循环Looper 55 } 56 } 57 58 }