Android线程间通信的Message机制
2010-06-01 15:44:00 来源:WEB开发网MessageQueue queue = me.mQueue;
while (true) {
Message msg = queue.next(); // might block
//if (!me.mRun) {
// break;
//}
if (msg != null) {
if (msg.target == null) {
// No target is a magic identifier for the quit message
return;
}
if (me.mLogging!= null)
me.mLogging.println(">>>>> Dispatching to " + msg.target + " "+ msg.callback + ": " + msg.what);
msg.target.dispatchMessage(msg);
if (me.mLogging!= null)
me.mLogging.println("<<<<< Finished to" + msg.target + " "+ msg.callback);
msg.recycle();
}
}
}
public static final void loop() {
Looper me = myLooper();
MessageQueue queue = me.mQueue;
while (true) {
Message msg = queue.next(); // might block
//if (!me.mRun) {
// break;
//}
if (msg != null) {
if (msg.target == null) {
// No target is a magic identifier for the quit message
return;
}
if (me.mLogging!= null)
me.mLogging.println(">>>>> Dispatching to " + msg.target + " "+ msg.callback + ": " + msg.what);
msg.target.dispatchMessage(msg);
if (me.mLogging!= null)
me.mLogging.println("<<<<< Finished to" + msg.target + " "+ msg.callback);
msg.recycle();
}
}
}
编辑本段 回目录1.4.Handler
Handler的构造函数表示Handler会有成员变量指向Looper和MessageQueue,后面我们会看到没什么需要这些引用;至于callback是实现了Callback接口的对象,后面会看到这个对象的作用。
Java代码
复制到剪贴板 Java代码
public Handler(Looper looper, Callback callback) {
mLooper = looper;
更多精彩
赞助商链接