How to deal with saving connection in android service

I use asmack for an IM application for Android, where I use a remote service with an AIDL interface.

Inside the onStartCommand method of my service, I write code as shown below. I create a connection and then connect to it using this. When someone launches my application inside the onCreate method of my application’s main activity, start my getApplicationContext.StartService(serviceIntent) . It works fine, but after a few minutes (sometimes 10 minutes and some time more than ten) messageListener , which I attach to the service stop to receive messages. But I know that the connection exists, because at the same time I use xmppConnection to send a message sending a message to user B, but he does not listen to messages from user B. I do not know why my listener stopped hearing the 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; } } 

Please guide.

+6
source share
1 answer

Is your connection closed by mistake if you haven’t noticed? You must add a connection listener and a log for each callback to debug the connection status.

You can install a zombie socket on Android: you can still write it, but the recipient will never receive the message, and, of course, you will not be able to read new messages from it. This can happen after the network status changes.

To detect that I am using XMPP Ping , it is triggered by the client from an alarm (every 15 minutes with inaccurate repetition). And I will turn off the empty space. This violates most of the timeout mechanisms that may exist between your client and your server (NAT or proxy). In addition, if you did not receive a ping response (for example, within 20s), you can assume that the connection is in a bad state and reconnects manually.

+4
source

Source: https://habr.com/ru/post/922963/


All Articles