Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Service : bind or start?

Tags:

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?

like image 356
pvllnspk Avatar asked Dec 04 '12 22:12

pvllnspk


People also ask

What does it mean to bind a service Android?

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).

What is the difference between startService and bindService?

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().

What is bound and unbound service in Android?

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.


1 Answers

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().

like image 187
Edward Falk Avatar answered Sep 27 '22 18:09

Edward Falk