title: Packet Listener in Android Service
body: <p>I am trying to create a service for a chat widget using XMPP, which picks up chat messages when they are sent to the user.</p>
  
  <p>I have created a service, and in the onStart I use a AsyncTask to connect to the chat server, and then sets up a packetlistener.</p>
  
  <p>Here's the code for the packer listener:</p>
  
  <pre><code>public void setConnection(XMPPConnection connection) {
      if (connection != null) {
          // Add a packet listener to get messages sent to us
          PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
          connection.addPacketListener(new PacketListener() {
              public void processPacket(Packet packet) {
                  Message message = (Message) packet;
                  if (message.getBody() != null) {
                      String fromName = StringUtils.parseBareAddress(message
                              .getFrom());
                      Log.v(TAG, "Got:" + message.getBody());
                      // messages.add(fromName + ":");
                      // messages.add(message.getBody());
  
                  }
              }
          }, filter);
      }
  }
  </code></pre>
  
  <p>The problem is it seems to stop listening after being idle for a while. If I send chat messages straight away they get output.</p>
  
  <p>Is the service getting stopped somehow? Is that the right place to put the packerlistener?</p>
  
  <p>Thanks</p>