Bachalo
Bachalo

Reputation: 7219

Android Services-when to use Context.bindService vs Context.startService

Can any Android experts explain when you would use

Context.bindService vs Context.startService to start a Service?

Upvotes: 2

Views: 2231

Answers (1)

Squonk
Squonk

Reputation: 48871

From the docs for Bound Services

A bound service is the server in a client-server interface. A bound service allows components (such as activities) to bind to the service, send requests, receive responses, and even perform interprocess communication (IPC).

In other words binding to a Service allows two-way interaction by exposing methods in the Service which are available through the IBinder via a ServiceConnection.

In contrast, using startService(...) performs more of a one-shot operation. This is only really useful if the Service can work in an autonomous fashion, i.e., it knows what it needs to do and doesn't need to be controlled further other than via any action and or data passed in the Intent used to start it. In general a Service which is started with startService(...) will not communicate directly with the component that started it (such as an Activity). It can however send data or results of an operation using a broadcast or by creating a Notification.

Upvotes: 4

Related Questions