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

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 callba

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");

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

Tags:Android 线程 通信

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