Skip to content
This repository has been archived by the owner on Apr 4, 2021. It is now read-only.

Should applications using aSmack use foreground Services?

Flowdalic edited this page Dec 28, 2014 · 2 revisions

No, never, please not! Use sticky Services!

In most cases, requesting the foreground priority for a Service is just plain wrong and against Android design principles. Use a sticky Service instead and restore your state when the service is killed and restarted, which usually only takes a few seconds, by Android.

Foreground services are meant for services where the termination of such would result in immediate negative user experience, e.g. because it plays an audio track in the background, not to prevent Android killing your Service, if it decides it is necessary. If you keep a XMPPConnection within an Android service, you can simply reconnect once the service is restored by Android.

When Android decides to kill a Service, it has a good reason to do so. Usually because it's under memory pressure and needs to reclaim resources. The terminated sticky Services are usually restarted when the pressure subsides, which happens often within a few seconds.

If create an XMPPConnection withing a long-living foreground Service you will produce a negative user experience, because Android will not kill the Service if it want's to reclaim resources. And long-living Services with hold an XMPPConnection instance are not guaranteed to be free of memory-leaks, resulting in the Service reclaiming more-and-more resources, slowing down the whole operating system.

Android 4.4.2: START_STICK Bug

Android 4.4.2 has a bug where a sticky service would not be restarted by the system:

This bug has been fixed with 4.4.3.

Clone this wiki locally