主线程发送消息到工作线程,工作线程的步骤是固定为3步的。
Looper.prepare();//步骤1,线程里使用handler必须这样写,
handler = new Handler(){//步骤2,先实例化handler
@Override
public void handleMessage(Message msg) {
String receiver = msg.obj.toString();
System.out.println("receiver:"+receiver+","+Thread.currentThread().getName());
}
};
Looper.loop();//步骤3 ,循环Looper
代码如下:
package com.wyl.handler_mars; import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
/**
* 主线程发送消息,工作线程接收消息
* @author Wyl
*
*/
public class SecondActivity extends Activity implements OnClickListener{
Button btn_fasong;
Handler hand;
Message msg;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
btn_fasong = (Button) findViewById(R.id.btn_send);
tv = (TextView) findViewById(R.id.tv_01);
btn_fasong.setOnClickListener(this);
Thread t = new MyThread();
t.start();
}
@Override
public void onClick(View v) {
//hand = new Handler();//这一行尤其重要,如果不注释掉,那么下面的线程中就没法接收到消息,因为这行代码中又重新生成了一个handler对象,也就对应着不同的looper,所以就没法
Message msg = hand.obtainMessage();
msg.obj = "无语了。。。";
System.out.println("onClick()...发送:"+"无语了。。。");
hand.sendMessage(msg);//主线程发送消息
} class MyThread extends Thread{ @Override
public void run() {
Looper.prepare();//获取一个Looper对象
System.out.println("===========");
hand = new Handler(){ @Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
System.out.println("MyThread.run()接收到了消息"+",所在线程:"+Thread.currentThread().getName());
}
};
Looper.loop();
} }
}
===============分割线===============
20151008重新总结,具体的说明在注释里有详细的解释,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <Button
android:id="@+id/btn_send"
android:onClick="doClick"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="发送" />
<Button
android:id="@+id/btn_Cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="取消发送" /> </LinearLayout>
MainActivity.java
package com.wyl.handler; 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.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity{
Button btn_fs;//发送
Button btn_qx;//取消
Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn_fs = (Button) findViewById(R.id.btn_send);
btn_qx = (Button) findViewById(R.id.btn_Cancel);
btn_fs.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Message msg = handler.obtainMessage();
String str = "我是wyl";
msg.obj = str;
System.out.println("onClick()发送:"+str+","+Thread.currentThread().getName());
handler.sendMessage(msg);
}
});
/*
* 这个线程里的handler在onClick()方法之前使用到,所以需要在线程的run()方法里
* 实例化,onClick()方法里就不用实例化了,也不能怪进行重新实例化,否则又是实例化的
* handler对象就不是一个了,handler不一致的话就会导致循环的队列不是同一个,就会
* 导致workThread里的handleMessage()方法没法接收到UIThread发出的消息
*/
MyThread t = new MyThread();
t.start(); } class MyThread extends Thread{
@Override
public void run() {
Looper.prepare();//步骤1,线程里使用handler必须这样写,
handler = new Handler(){//步骤2,先实例化handler
@Override
public void handleMessage(Message msg) {
String receiver = msg.obj.toString();
System.out.println("receiver:"+receiver+","+Thread.currentThread().getName());
}
};
Looper.loop();//步骤3 ,循环Looper
}
} }