Android Service : bind or start?-ThrowExceptions

Exception or error:

In what cases should I start Service and in what case bind Service? For example – an android client for Music Service? Are the any differences in the priority for the System;are the any common rules; anything else?

How to solve:

Use startService() for services which will run independently after you start them. Music players are a good example. These run until they call stopSelf() or someone calls stopService().

You can communicate with a running service by sending Intents back and forth, but for the most part, you just start the service and let it run on its own.

Use bind() when the service and client will be communicating back and forth over a persistent connection. A good example is a navigation service which will be transmitting location updates back to the client. Binders are a lot harder to write than intents, but they’re really the way to go for this usage case.

Regarding the priority: When all activities of a process lose their visibility, the process becomes a service process if it hosts a service which was started with onStart(), otherwise it becomes a background process. Service processes have a higher priority than background processes. Further details at the android developer site.

If a service process without visible activity needs a higher priority (e.g. a music player which really should not be interrupted), the service can call startForeground().

Leave a Reply

Your email address will not be published. Required fields are marked *