首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将消息从服务发送到UI-Thread -消息在服务的处理程序中延迟

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

Stack Overflow用户
提问于 2012-06-20 05:09:30
回答 1查看 1.1K关注 0票数 3

我有一个从服务器拉取数据的服务。我作为gui有需要更新的活动。

因此,在不同的情况下,例如成功地从服务器拉出信息,服务器必须向活动发送信号,表示有新的信息。

为此,我实现了这个函数:

代码语言:javascript
复制
    /**
 * 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);
    }
}

为了处理这些消息,我使用了消息处理程序:

代码语言:javascript
复制
    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线程。

有什么建议吗?

非常感谢!

EN

回答 1

Stack Overflow用户

发布于 2013-05-24 17:52:46

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11109475

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档