Android线程间通信的Message机制
2010-06-01 15:44:00 来源:WEB开发网核心提示:obj = "OwnLooperThread has no looper and handleMessage function executed in main thread!";}else {mOwnLooperThreadHandler = new EventHandler(myLooper);
obj = "OwnLooperThread has no looper and handleMessage function executed in main thread!";
}
else {
mOwnLooperThreadHandler = new EventHandler(myLooper);
obj = "This is from OwnLooperThread self and handleMessage function executed in NoLooperThread!";
}
mOwnLooperThreadHandler.removeMessages(0);
//给自己发送消息
Message m = mOwnLooperThreadHandler.obtainMessage(3, 1, 1, obj);
mOwnLooperThreadHandler.sendMessage(m);
Looper.loop();
}
}
//ReceiveMessageThread has his own message queue by execute Looper.prepare();
class ReceiveMessageThread extends Thread{
public void run() {
Looper.prepare();
mOtherThreadHandler = new Handler(){
public void handleMessage(Message msg) {
Log.e(sTag, (String)msg.obj);
}
};
Looper.loop();
}
}
更多精彩
赞助商链接