Android线程间通信的Message机制
2010-06-01 15:44:00 来源:WEB开发网mQueue = looper.mQueue;
mCallback = callback;
}
public interface Callback {
public boolean handleMessage(Message msg);
}
public Handler(Looper looper, Callback callback) {
mLooper = looper;
mQueue = looper.mQueue;
mCallback = callback;
}
public interface Callback {
public boolean handleMessage(Message msg);
}
获取消息:直接通过Message的obtain方法获取一个Message对象。
Java代码
复制到剪贴板 Java代码
public final Message obtainMessage(int what, int arg1, int arg2, Object obj){
return Message.obtain(this, what, arg1, arg2, obj);
}
lic final Message obtainMessage(int what, int arg1, int arg2, Object obj){
return Message.obtain(this, what, arg1, arg2, obj);
发送消息:通过MessageQueue的enqueueMessage把Message对象放到MessageQueue的接收消息队列中
Java代码
复制到剪贴板 Java代码
public boolean sendMessageAtTime(Message msg, long uptimeMillis){
boolean sent = false;
MessageQueue queue = mQueue;
if (queue != null) {
msg.target = this;
sent = queue.enqueueMessage(msg, uptimeMillis);
} else {
RuntimeException e = new RuntimeException(this + " sendMessageAtTime() called with no mQueue");
Log.w("Looper", e.getMessage(), e);
}
return sent;
}
public boolean sendMessageAtTime(Message msg, long uptimeMillis){
boolean sent = false;
MessageQueue queue = mQueue;
if (queue != null) {
msg.target = this;
sent = queue.enqueueMessage(msg, uptimeMillis);
} else {
RuntimeException e = new RuntimeException(this + " sendMessageAtTime() called with no mQueue");
更多精彩
赞助商链接