-
Notifications
You must be signed in to change notification settings - Fork 575
Should applications using aSmack use foreground Services?
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 has a bug where a sticky service would not be restarted by the system:
- http://stackoverflow.com/questions/20636330/start-sticky-does-not-work-on-android-kitkat-edit-and-jelly-bean
- https://code.google.com/p/android/issues/detail?id=63793
This bug has been fixed with 4.4.3.