62

I would like to know when it is smart to use bindService and when to use startService.

For example:

If I use bindService with BIND_AUTO_CREATE, the service will be started and created automatically as is written here: http://developer.android.com/reference/android/content/Context.html#BIND_AUTO_CREATE

When is it smart then to use bindService and when startService? I really don't understand these two correctly.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
senzacionale
  • 20,448
  • 67
  • 204
  • 316

2 Answers2

101

You usually use bindService() if your calling component(Activity) will need to communicate with the Service that you are starting, through the ServiceConnection. If you do not want to communicate with the Service you can use just startService(). You Can see below diffrence between service and bind service.

From the docs :

Started

A service is "started" when an application component (such as an activity) starts it by calling startService(). Once started, a service can run in the background indefinitely, even if the component that started it is destroyed. 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.

Bound

A service is "bound" when an application component binds to it by calling bindService(). A bound service offers a client-server interface that allows components to interact with the service, send requests, get results, and even do so across processes with interprocess communication (IPC). A bound service runs only as long as another application component is bound to it. Multiple components can bind to the service at once, but when all of them unbind, the service is destroyed.

You can read more here : Android Services, Bound Services

Eido95
  • 1,313
  • 1
  • 15
  • 29
Ovidiu Latcu
  • 71,607
  • 15
  • 76
  • 84
  • I find it easier to think in terms association. bound service will be available only if there is any association/client/activity alive so makes sense in media player where in if its UI is destroyed we should also close the player service. startedservice continue to run even if its association is destroyed so makes sense in printer service where in application fires print and can exit while printer service will do its job. – Miten Dec 04 '18 at 13:40
28

I agree with @Ovidiu Latcu but with one important note: when using bound services, the service is ended when the activity that started it is ended, (if it is the only activity bound to that service).

So if you want to run your service at the background while the app is in the background, (the activity is paused for example and not visible to the user) then you must start the service without bounding to it and communicate with it with BroadcastReceiver for example.

benchuk
  • 669
  • 7
  • 13
  • 3
    also you can start the service and bound to it later - this way it will stay running until stop is called. – benchuk Mar 01 '16 at 19:10
  • 1
    I would consider getting result (using `BroadcastReceiver` or any other alternative) from a service which is only started with `startService()` and there is no binding between him and the clients as a **bad practice** because, as Ovidiu Latcu quoted, a **started** service usually _does not return a result_ to the caller (clients), in contrast to **bound** service which offers a client-server interface that allows components to interact with the service, send requests, _receive results_... . – Eido95 Nov 09 '16 at 10:11
  • I would only use `BroadcastReceiver` (or any other alternative) when I would want to _receive results_ from components which are NOT services (e.g. `DownloadManager`, `BluetoothManager`, or any other "not a service" class that broadcast results). There is a reason why Android developers has implemented the concept of service binding. – Eido95 Nov 09 '16 at 11:05
  • http://stackoverflow.com/questions/3514287/android-service-startservice-and-bindservice – benchuk Nov 21 '16 at 16:41
  • This answer is not correct, because as describe in Google document, startService() tell the system about something doing in background. – Phuoc Huynh Mar 09 '17 at 03:56