2012-06-19 48 views
3

我有一个服务正在从服务器提取数据。我有活动作为gui需要更新。从服务发送消息到UI线程 - 在服务处理程序中延迟的消息

因此,在不同的场合,例如信息已成功从服务器中提取出来,服务器必须通知有新信息的活动。

为此,我实现了这个功能:

/** 
* 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线程而没有问题。

有什么建议吗?

非常感谢!

+0

想想消息碰撞。可能会有其他人发送很多消息并阻止/创建此消息的延迟。 –

+0

不,这不是消息接收者(UI Thread)的问题,问题发生在服务端。只有服务可以从其内部发送消息。 – user929995

回答

0

我不能确定没有更多的代码,但它听起来像是一个线程问题。

尝试将您的处理程序代码放入单独的HandlerThread中,检索服务中的处理程序并将消息发送给它。