如何处理好保持Android的服务活动连接 [英] how to handle keep alive connection in android Service

查看:161
本文介绍了如何处理好保持Android的服务活动连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用asmack一个机器人IM应用程序,在这里我使用远程服务与AIDL接口。

I am using asmack for an android IM application, where I am using remote service with AIDL interface.

onStartCommand 我的服务的方法,我写code如下。我创建的连接,然后使用该登录。当人遇到我的的onCreate 我的应用程序运行我的服务 getApplicationContext.StartService(serviceIntent)的主要活动的方法中的应用。它工作正常,但几分钟(有时是10分钟,一段时间后十余)后消息监听,我附上服务站内接收消息。但我知道,连接存在,因为我同时使用 xmppConnection 来发送消息它发送消息到用户B,但它不听从用户B.我不知道的消息为什么我的听众听到停止消息。

Inside of onStartCommand method of my service I write code as below. I create connection and then login using that. When anyone run my application inside onCreate method of main activity of my application run my service getApplicationContext.StartService(serviceIntent). It's working fine, but after few minutes (sometimes 10 minutes and some time more than ten) messageListener that I attach inside of service stops to receive messages. But I know that the connection exist, because same time I use xmppConnection to send message it's sending message to user B but it not listening messages from user B. I don't know why my listener stop hearing message.

public int onStartCommand(final Intent intent, final int flags, final int startId) {
    ConnectionConfiguration config = new ConnectionConfiguration(URL, MyPort, Host);
    xmppConnection = new XMPPConnection(config);
    xmppConnection.connect();
    xmppConnection.login("someid@sample.com", "testpass");
    xmppConnection.addPacketListener(myMessageListener, new PacketTypeFilter(Message.class));
    return START_STICKY;
}
private PacketListener myMessageListener = new PacketListener() {
    public void processPacket(Packet packet) {
        Message msg = (Message) packet;
    }
}

请指导。

推荐答案

是连接在被关闭的错误,你没有注意到?
你应该为每个回调添加连接监听器和日志调试连接状态。

Is your connection being closed on error without you noticing ? You should add a connection listener and a log for each callback to debug the connection state.

在Android上很可能有一个僵尸插座:你仍然可以写,但收件人将永远不会收到消息,当然,你将无法从中读取新邮件。
它可以在网络状态更改后发生。

On Android it's possible to have a "zombie" socket: you can still write to it, but the recipient will never receive the messages, and of course you won't be able to read new messages from it. It can happen after a network status change.

要检测,我使用 XMPP平,从客户端发起,从报警(每15分钟重复不精确)。我禁用空白存留。
这违背了大多数可以在客户端和服务器(NAT或代理)之间存在的超时机制。
另外,如果你没有收到任何答案平(20岁例如内),你可以假设连接是在一个糟糕的状态,手动重新连接。

To detect that I use XMPP Ping, initiated from the client from an alarm (every 15 minutes with inexact repeating). And I disable white space keep alive. This defeats most of the timeout mechanisms that can exist between your client and your server (NAT or proxies). Plus, if you don't receive any answer to the ping (within 20s for example), you can assume the connection is in a bad state and reconnect manually.

这篇关于如何处理好保持Android的服务活动连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆