******************************************************
HandlerThread Handler 多线程handler和message消息发送
******************************************************
handler和message都有发送消息给handlerMessage(Message msg)消息处理方法的接口,但其接口调用有所不同
1、handler发送接口使用:
Message message=new Message();
message.what=1;
handler.sendMessage(message);
2、message发送接口使用:
Message message=handler.obtainMessage(); //绑定message与handler
message.what = 0;
message.sendToTarget(); //发送到handlerMessage(Message msg)
或者:
Message message=handler.obtainMessage(handler,what);//绑定message与handler
message.what = 0;
message.sendToTarget(); //发送到handlerMessage(Message msg)
例如:
public class Message extends Activity{
final String TAG = "Message";
Handler mHandler = new Handler(){
public void handleMessage(Message msg){
switch(msg.what){
case 0:
Log.d(TAG,"Message---------msg.what=0");
break;
case 1:
Log.d(TAG,"Message---------msg.what=1");
break;
default:
break;
}
}
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mhandersend();
messagesend();
}
void messagesend(){
new Thread(){
public void run(){
Message msg = Message.obtain(mHandler,0);
msg.what = 0;
msg.sendToTarget();
}
}.start();
}
void mhandersend(){
new Thread(){
public void run(){
Message msg = new Message();
msg.what = 1;
handler.sendMessage(msg);
}
}.start();
}
}
阅读(962) | 评论(0) | 转发(0) |