6

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

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;
    }
}

Please guide.

stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
aftab
  • 1,141
  • 8
  • 21
  • 40
  • Please format your code and choose a good title for your post. – Flow Aug 16 '12 at 06:23
  • Problem is that my connection exist but it stop to Listen the Packet.using that connection I call sendPacket that working fine but don't know why it stop to listen incoming packet.I check my service also running at backend because I can sendPacket from service to send message packet,Please guide. I am facing same problem as posted on below link. http://stackoverflow.com/questions/5040852/packet-listener-in-android-service – aftab Aug 18 '12 at 00:37
  • Your posted link seems to be answered ---->http://stackoverflow.com/questions/5040852/packet-listener-in-android-service – sascha10000 Aug 18 '12 at 01:36
  • No.I follow that steps also.It looks My service remain alive but problem comes with xmpp connection listener.it stops to listen packets.and Logcat nothing showing related to destroy of service . – aftab Aug 18 '12 at 01:48

1 Answers1

4

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.

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.

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.

Guillaume Perrot
  • 4,278
  • 3
  • 27
  • 37
  • Dear Guillaume Perrot: thanks for your reply. I am facing a strange issue. I am using AIDL service to handle my keep alive connection. I am sending and receiving messages that is working fine. But using the XMPP object I send subscription invitation request to a user it send request packet fine. But here problem come its packet listener stop to listen incoming packets. Same time I try to send packet using same xmpp object it sending ok but receiving stop to hear incoming packet. It looks xmpp packet listener disturb after send invitation request. Please guide. . – aftab Aug 21 '12 at 11:45
  • The AndroidDebugger class (ConsoleDebugger on regular Smack) can be used to print what is actually read or written on the socket without using the PacketListener API. You should check if you receive packets in the debugger's logs when your listener stops working. – Guillaume Perrot Sep 05 '12 at 12:25