/创建一个子线程,用于主线程向子线程发送消息
class MyThread extends Thread {
Handler handler=null;
public Handler getHandler(){
return handler;
}
@Override
public void run() {
Looper.prepare();
handler=new Handler(){
@Override
public void handleMessage(Message msg) {
Log.d("子线程",msg.arg1+"");
}
};
Looper.loop();
}
}
}
case R.id.btn3:
//将线程申明为全局变量 使用第一种发送消息方法
//注意:哪个线程给哪个线程发,就调用接收方的handler
Message message =thread.getHandler().obtainMessage()
message.arg1=89
thread.getHandler().sendMessage(message)
break
}
thread=new MyThread();
thread.start();