package com.example.test_example;
import android.app.Activity;
import android.os.Bundle;
import android.os.Looper;
import android.os.Message;
import android.widget.TextView;
/**
* Demo描述:
*
* 示例步骤如下:
* 1 子线程给子线程本身发送消息
* 2 收到1的消息后,子线程给主线程发送消息
* 3 收到2的消息后,主线程给子线程发送消息
*
* 为实现子线程给自己本身发送消息,关键还是在于构造Handler时传入的Looper.
* 在此就传入该子线程自己的Looper即调用Looper.myLooper(),代码如下:
* Looper.prepare();
* mHandlerTest1=new HandlerTest1(Looper.myLooper());
* Looper.loop();
*
* 所以当mHandlerTest1.sendMessage(message);发送消息时
* 当然是发送到了它自己的消息队列.
*
* 当子线程中收到自己发送的消息后,可继续发送消息到主线程.此时只要注意构造
* Handler时传入的Handler是主线程的Handler即可,即getMainLooper().
* 其余没啥可说的.
*
*
* 在主线程处理消息后再发消息到子线程
*
*
* 其实这些线程间发送消息,没有什么;关键还是在于构造Handler时传入谁的Looper.
*
*/
public class MainActivity extends Activity {
private TextView textView;
private TextView textView1;
HandlerThread1 handlerThread1;
HandlerThread2 handlerThread2;
private int counter=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=(TextView)findViewById(R.id.text);
textView1=(TextView)findViewById(R.id.text1);
init();
}
private void init(){
new Thread(){
@Override
public void run() {
super.run();
Looper.prepare();
handlerThread1=new HandlerThread1(Looper.myLooper());
Message msg=new Message();
msg.obj="这是子线程发送的消息";
handlerThread1.sendMessage(msg);
Looper.loop();
};
}.start();
}
private class HandlerThread1 extends android.os.Handler{
public HandlerThread1(Looper myLooper) {
super(myLooper);
}
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
System.out.print("子线程收到:"+msg.obj);
textView1.setText(msg.obj+"");
handlerThread2=new HandlerThread2(getMainLooper());
Message message=new Message();
message.obj="哈哈";
handlerThread2.sendMessage(message);
}
}
private class HandlerThread2 extends android.os.Handler{
private HandlerThread2(Looper mainLooper) {
super(mainLooper);
}
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
textView.setText(msg.obj+"");
//3 收到消息后再发消息到子线程
if (counter==0) {
Message message = new Message();
message.obj = "主线程发送的消息Xi~Xi";
handlerThread1.sendMessage(message);
counter++;
}
}
}
}

浙公网安备 33010602011771号