Looper 是通道,在Handler处理Message的时候,实际上都是需要依靠一个Looper通道来完成的,当用户取得一个handler对象的时候实际上都是通过Looper完成的,在一个Activity类之中,会自动的帮助用户启动好Looper对象,而如果在一个用户自定义的类中,则需要用户手工使用调用Looper类中的若干方法之后才可以正常的启动looper对象
下面的程序是主线程与子线程之间的交互传数据
主程序MyDemo.java 修改UI只能在主线程中完成,子线程是处理主线程交给的一些费时的操作package com.jackie.mythreaddemo;
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;
import android.widget.TextView;
public class MyDemo extends Activity {
private static final int SETMAIN = 1; // 设置what标志
private static final int SETCHILD = 2; // 设置what标志
private Handler mainHandler, childHandler;
private TextView msg = null;
private Button but = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.msg = (TextView) super.findViewById(R.id.msg);
this.but = (Button) super.findViewById(R.id.but);
new Thread(new childThread(), "Child Thread").start();
MyDemo.this.mainHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch(msg.what){
case SETMAIN:
MyDemo.this.msg.setText("主线程接收子线程传过来的消息:"+msg.obj.toString());
break;
}
}
};
this.but.setOnClickListener(new OnClickListenerImpl());
}
private class OnClickListenerImpl implements OnClickListener {
@Override
public void onClick(View arg0) { // 将消息发送到子线程
if (MyDemo.this.childHandler != null) {
Message childMsg = MyDemo.this.childHandler.obtainMessage(); // 创建消息
childMsg.obj = MyDemo.this.mainHandler.getLooper().getThread()
.getName()
+ "-->hello jackie.";
childMsg.what = SETCHILD;
MyDemo.this.childHandler.sendMessage(childMsg);
}
}
}
class childThread implements Runnable {
@Override
public void run() { // 子线程
Looper.prepare();
MyDemo.this.childHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case SETCHILD: // 子线程接收主线程发送来的消息
System.out.println("****Main Child Message:" + msg.obj);
Message toMain = MyDemo.this.mainHandler
.obtainMessage();
toMain.obj = "\n\n[B]这是子线程发送给主线程的信息:"
+ MyDemo.this.childHandler.getLooper()
.getThread().getName();
toMain.what=SETMAIN;
MyDemo.this.mainHandler.sendMessage(toMain);
break;
}
}
};
Looper.loop(); // 创建消息队列
}
}
@Override
protected void onDestroy() {
super.onDestroy();
MyDemo.this.childHandler.getLooper().quit();
}
}
package com.jackie.mythreaddemo;
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;
import android.widget.TextView;
public class MyDemo extends Activity {
private static final int SETMAIN = 1; // 设置what标志
private static final int SETCHILD = 2; // 设置what标志
private Handler mainHandler, childHandler;
private TextView msg = null;
private Button but = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.msg = (TextView) super.findViewById(R.id.msg);
this.but = (Button) super.findViewById(R.id.but);
new Thread(new childThread(), "Child Thread").start();
MyDemo.this.mainHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch(msg.what){
case SETMAIN:
MyDemo.this.msg.setText("主线程接收子线程传过来的消息:"+msg.obj.toString());
break;
}
}
};
this.but.setOnClickListener(new OnClickListenerImpl());
}
private class OnClickListenerImpl implements OnClickListener {
@Override
public void onClick(View arg0) { // 将消息发送到子线程
if (MyDemo.this.childHandler != null) {
Message childMsg = MyDemo.this.childHandler.obtainMessage(); // 创建消息
childMsg.obj = MyDemo.this.mainHandler.getLooper().getThread()
.getName()
+ "-->hello jackie.";
childMsg.what = SETCHILD;
MyDemo.this.childHandler.sendMessage(childMsg);
}
}
}
class childThread implements Runnable {
@Override
public void run() { // 子线程
Looper.prepare();
MyDemo.this.childHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case SETCHILD: // 子线程接收主线程发送来的消息
System.out.println("****Main Child Message:" + msg.obj);
Message toMain = MyDemo.this.mainHandler
.obtainMessage();
toMain.obj = "\n\n[B]这是子线程发送给主线程的信息:"
+ MyDemo.this.childHandler.getLooper()
.getThread().getName();
toMain.what=SETMAIN;
MyDemo.this.mainHandler.sendMessage(toMain);
break;
}
}
};
Looper.loop(); // 创建消息队列
}
}
@Override
protected void onDestroy() {
super.onDestroy();
MyDemo.this.childHandler.getLooper().quit();
}
}