繁体   English   中英

从服务向UI-Thread发送消息 - 在服务处理程序中延迟的消息

[英]Sending message from service to UI-Thread - Messages delayed in service's handler

我有一个从服务器提取数据的服务。 我有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线程。

有什么建议么?

非常感谢!

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM