我有一个从服务器拉取数据的服务。我作为gui有需要更新的活动。
因此,在不同的情况下,例如成功地从服务器拉出信息,服务器必须向活动发送信号,表示有新的信息。
为此,我实现了这个函数:
/**
* ServerService.class
*
* Sends a message to the observer that there is a new status message to be
* pulled from the service#
*
* @param pMessage
* the message to be set to the public status
*/
private void signalNewStatus(String pMessage) {
//check if there is a message at all to be signaled as new
if (pMessage != null) {
Log.v(tag, "signalNewStatus:" + pMessage);
// set the message
vStatus = pMessage;
// start the new callback via the handler
myMessageHandler.sendEmptyMessage(I_STATUS_UPDATE_SIGNAL);
}
}为了处理这些消息,我使用了消息处理程序:
private final Handler myMessageHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
Log.i(tag, "handleMessage: "+msg.what);
switch (msg.what) {
case I_STATUS_UPDATE_SIGNAL: {
// Broadcast to all clients the new value.
final int N = myCallbackList.beginBroadcast();
for (int i = 0; i < N; i++) {
try {
myCallbackList.getBroadcastItem(i).sendUpdateStatusSignal(true);
} catch (RemoteException e) {
}
}
myCallbackList.finishBroadcast();
}
break;第一个函数经常被调用,我可以在日志中看到它在时间上正常工作。
但是消息处理的日志记录不会立即被调用。它会被延迟,并且在结束时被调用的次数最多。不知何故就像大块头一样。
我不明白为什么会这样。其余的工作都很好,一旦消息被处理,消息就会顺利地到达UI线程。
有什么建议吗?
非常感谢!
发布于 2013-05-24 17:52:46
如果没有更多的代码,我不能确定,但这听起来像是一个线程问题。
尝试将处理程序代码放在单独的HandlerThread中,在服务中检索处理程序并向其发送消息。
https://stackoverflow.com/questions/11109475
复制相似问题