Kevin
Kevin

Reputation: 2730

Is it bad practice to use multiple background services in a single android app?

I'd like to keep some things separate from each other (in this case a 15-seconds interval task that polls a database, and a third party library that receives push notifications from our sip server). I'd like to keep them separate mainly to keep the code more readable, but I also think it's cleaner in general to keep separate tasks, you know, separated...
This said, is it bad practice to use multiple services for this? Is it perhaps better to use one service with multiple threads? Is it even possible to use more than one service?
I haven't really tried anything yet, I'm not eager to rewrite the code if it might be a bad thing to do.

Upvotes: 10

Views: 2595

Answers (1)

Kurt Huwig
Kurt Huwig

Reputation: 1013

First of all, a service does not imply that a separate thread is running, but I guess this is what you want to do. If you run several threads, there is no way of the AndroidOS to terminate them besides killing the whole Dalvik VM. And this means that you have no way of knowing when you are about to be terminated. If you have a service with a thread and use proper life-cycle management, i.e. kill the thread when Android notifies the service that it is about to stop it, then it is easy to maintain state.

Regarding your question: use several services with one thread each

Upvotes: 7

Related Questions