Keep XMPP Connection (using Asmack) Alive On Android
Solution 1:
I am not sure if "garbage collected" is the right term here. It is more likely that your activity simply gets closed by Android, because you create the connection in an Activity.
But you are right, in order to keep a stable connection you need to put the XMPP connection into a Service. But make sure that the connection is in an extra thread, because a service for itself is not a extra process or thread. This could be done, for example with an Handler.
HandlerThread thread = new HandlerThread(SERVICE_THREAD_NAME);
thread.start();
handlerThreadId = thread.getId();
serviceLooper = thread.getLooper();
serviceHandler = new ServiceHandler(serviceLooper);
Then you can communicate with the Service Thread via messages/intents. Another alternative would be a Binder interface.
You could also have a look on how others do this: Beem and GTalkSMS are both open source projects that implement an XMPP connection with an Android service. GTalkSMS uses the IntentService approach, whereas Beem uses an Binder to communicate with the XmppConnection.
Note that we achive a pretty stable connection with GTalkSMS, but it's never so good as the stock GTalk Service/Client. One reason is that smacks default keep-alive check timer is 5 minutes, which is way to often for a mobile device when you want to save as much battery as possible. So I had to set it to a higher value, which comes with some drawbacks. You will always have a trade-off between a fast, responsive and stable connection vs. battery life when it comes to this.
Post a Comment for "Keep XMPP Connection (using Asmack) Alive On Android"