WEB开发网
开发学院手机开发Android 开发 Android线程间通信的Message机制 阅读

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) {/

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;

上一页  1 2 3 4 5 6 7 8  下一页

Tags:Android 线程 通信

编辑录入:coldstar [复制链接] [打 印]
赞助商链接