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?
A bound service is the server in a client-server interface. It allows components (such as activities) to bind to the service, send requests, receive responses, and perform interprocess communication (IPC).
Usually, a started service performs a single operation and does not return a result to the caller. For example, it might download or upload a file over the network. When the operation is done, the service should stop itself. A service is "bound" when an application component binds to it by calling bindService().
Bounded services are bounded to an activity which binds it and will work only till bounded activity is alive. while a unbounded service will work till the completion even after activity is destroyed.
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()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With