Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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.</p> <p>But you are right, in order to keep a stable connection you need to put the XMPP connection into a <a href="http://developer.android.com/reference/android/app/Service.html">Service</a>. 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 <a href="http://developer.android.com/reference/android/os/Handler.html">Handler</a>.</p> <pre><code>HandlerThread thread = new HandlerThread(SERVICE_THREAD_NAME); thread.start(); handlerThreadId = thread.getId(); serviceLooper = thread.getLooper(); serviceHandler = new ServiceHandler(serviceLooper); </code></pre> <p>Then you can communicate with the Service Thread via messages/intents. Another alternative would be a <a href="http://developer.android.com/reference/android/os/IBinder.html">Binder</a> interface.</p> <p>You could also have a look on how others do this: <a href="http://www.beem-project.com/projects/beem/repository">Beem</a> and <a href="http://code.google.com/p/gtalksms/source/browse/">GTalkSMS</a> are both open source projects that implement an XMPP connection with an Android service. GTalkSMS uses the <a href="http://code.google.com/p/gtalksms/source/browse/src/com/googlecode/gtalksms/MainService.java#115">IntentService approach</a>, whereas Beem uses an <a href="http://www.beem-project.com/projects/beem/repository/entry/src/com/beem/project/beem/service/aidl/IXmppConnection.aidl">Binder</a> to communicate with the XmppConnection.</p> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload