主线程给主线程发送消息:
public class mainSendToMain extends Activity {
private Button btnTest;
private TextView textView;
private Handler handler;
@Override
public void 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(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Looper looper = Looper.getMainLooper();
//
getMainLooper()方法是返回
主线程的Looper对象
//这里以主线程的Looper对象创建了handler,
//所以,这个handler发送的Message会被传递给主线程的MessageQueue。
handler = new MyHandler(looper);
//构建Message对象
//第一个参数:是自己指定的message代号,方便在handler选择性地接收
//第二三个参数是传递的整型值,对内存消耗较少
//第四个参数需要封装的对象
Message msg = handler.obtainMessage(1,2,3,"主线程发消息了");
handler.sendMessage(msg); //发送消息
}
});
}
class MyHandler extends Handler{
public MyHandler(Looper looper){
super(looper);
}
public void handleMessage(Message msg){
textView.setText("我是主线程的Handler,收到了消息:"+msg.obj);
}
}
其他线程给自己发送消息:
public class otherSendToIts extends Activity{
private Button btnTest;
private TextView textView;
private Handler handler;
@Override
public void 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(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
//启动线程
new MyThread().start();
}
});
}
class MyHandler extends Handler{
public MyHandler(Looper looper){
super(looper);
}
public void handleMessage(Message msg){
super.handleMessage(msg);
textView.setText((String)msg.obj);
}
}
class MyThread extends Thread{
public void run(){
Looper.prepare();
//创建该线程的Looper对象
handler = new ThreadHandler(Looper.myLooper());
//这里Looper.myLooper()获得的就是该线程的Looper对象了
Message msg = handler.obtainMessage(1,1,1,"我自己");
handler.sendMessage(msg);
Looper.loop();
}
//定义线程类中的消息处理类
class ThreadHandler extends Handler{
public ThreadHandler(Looper looper){
super(looper);
}
public void handleMessage(Message msg){
//这里对该线程中的MessageQueue中的Message进行处理
//这里我们再返回给主线程一个消息
//加入判断看看是不是该线程自己发的信息
if(msg.what == 1 && msg.obj.equals("我自己")){
handler = new MyHandler(Looper.getMainLooper());
Message msg2 = handler.obtainMessage(1,1,1,"禀告主线程:我收到了自己发给自己的Message");
handler.sendMessage(msg2);
}
}
}
}
}
其他线程给主线程发送消息:
public class otherSendToMain extends Activity{
private Button btn;
private TextView textView;
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.btn_01);
textView = (TextView) findViewById(R.id.view_01);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new MyThread().start();
}
});
}
class MyHandler extends Handler{
public MyHandler(Looper looper){
super(looper);
}
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
textView.setText("我是主线程的Handler,收到了消息:"+(String)msg.obj);
}
}
class MyThread extends Thread{
public void run(){
Looper looper = Looper.getMainLooper();
handler = new MyHandler(looper);
Message message = handler.obtainMessage(1,2,3,"其他线程发消息了");
handler.sendMessage(message);
}
}
}
主线程给其他线程发送消息:
public class mainSendToOther extends Activity {
Button button3;
TextView textView;
Looper mainLooper;
// 其它线程
OtherHandler otherHandler;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button3 = (Button) findViewById(R.id.btn_01);
textView = (TextView) findViewById(R.id.view_01);
button3.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Message msg =otherHandler.obtainMessage(1,"这是主线程发送的消息");
otherHandler.sendMessage(msg);
}});
MyThread thread = new MyThread();
thread.start();
}
class MyThread extends Thread
{
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
//创建该线程的Looper对象,用于接收消息
Looper.prepare();
//线程的looper创建的handler表示消息接收者是子线程
otherHandler = new OtherHandler(Looper.myLooper());
//循环从MessageQueue中取消息。
Looper.loop();
}
}
class OtherHandler extends Handler
{
public OtherHandler() {
// TODO Auto-generated constructor stub
}
public OtherHandler(Looper looper) {
// TODO Auto-generated constructor stub
super(looper);
}
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
mainHandler = new MainHandler(Looper.getMainLooper());
Message message = new Message();
message.obj="子线程收到消息";
mainHandler.sendMessage(message);
}
}
MainHandler mainHandler;
class MainHandler extends Handler
{
public MainHandler() {
// TODO Auto-generated constructor stub
}
public MainHandler(Looper looper) {
// TODO Auto-generated constructor stub
super(looper);
}
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
textView.setText("我是主线程,这是收到的消息:"+msg.obj);
}
}
}