5

As everyone might know, there are two primary types of services in Android: started and bound (I'm not counting started and bound services, as they're mostly the same as just started services).

You can find tons of tutorials on how to use bound services or how to bind to started service, but there are actually no answer on why would anyone use bound (not-started) services within the application process (in other words - without IPC)?

Is there any (hidden?) profit from using bound service (let's say for some sort of processing) over using standard threading tools (AsyncTaks, Executors, plain threads)? Would it worth boilerplate code for connection of such service?

Some context

Question appeared after digging through sources of Google Camera. They're creating a bound (once again - not-started) service for saving images. What is the point? Why not just used some Executor? Am I missing something important?

If that is a bound service, then there is no way it would help to persist saving progress while device configuration is changing (i.e. device is rotated). So I see no advantages.

Dmitry Zaytsev
  • 23,650
  • 14
  • 92
  • 146
  • you mean bound local services? – pskink Sep 04 '14 at 15:30
  • look into [this](http://stackoverflow.com/questions/9272217/service-or-bound-service) or [this post](http://stackoverflow.com/questions/16162002/bound-service-versus-started-service-on-android-and-how-to-do-both) – Sagar Pilkhwal Sep 04 '14 at 15:30
  • @SagarPilkhwal first link is a bit closer to the answer, but still it does not describe why should you use such Service in the first place. Why not just launch some `HandlerThread` instead? Second link just out of the topic as it describes bound **and** started services. – Dmitry Zaytsev Sep 04 '14 at 16:10
  • @pskink yes, bound (not started) services. Sorry for my spelling, I'll fix that. – Dmitry Zaytsev Sep 04 '14 at 16:10
  • @DmitryZaitsev i can't believe you didn't read that http://developer.android.com/guide/components/bound-services.html#Binder, see LocalService/LocalBinder – pskink Sep 04 '14 at 16:25
  • 1
    @pskink I did read that, of course :) My question is not "how", my question is "why I need that kind of service?". Why not just use `Executor`, or `HandlerThread`, or whatever within `Activity`? – Dmitry Zaytsev Sep 04 '14 at 16:30
  • @pskink I added some context to my question. I hope it will be more clear now – Dmitry Zaytsev Sep 04 '14 at 16:34
  • @DmitryZaitsev since activity can go kaput if memory is low and with it are going kaput Executor , or HandlerThread, or whatever – pskink Sep 04 '14 at 16:38
  • 2
    @pskink `Service` will also go down then, as it bound to this particular `Activity`. Otherwise it would stay alive "forever". – Dmitry Zaytsev Sep 04 '14 at 16:41
  • yes but activities are like private soldiers, they are on the first line of the front, services are like marshals/generals, so the services are going down in the last order – pskink Sep 04 '14 at 16:49
  • @pskink is it documented somewhere? – Dmitry Zaytsev Sep 04 '14 at 16:54
  • @DmitryZaitsev http://developer.android.com/guide/components/services.html ^F kill also i read somewhere even better explanation but don't remember right now, will let you know when found – pskink Sep 04 '14 at 17:04
  • @DmitryZaitsev hehe it was a direct link from the previous url http://developer.android.com/guide/components/processes-and-threads.html – pskink Sep 04 '14 at 17:23

1 Answers1

2

Started Service is useful in case where there is need for no or very limited (one-way) interaction between starting component i.e. Activity or BroadcastReceiver, and the started service. For instance, a fire-and-forget background download. You give the downloader service a URL, start the service and forget all about it. The only way the downloader service ever interacts with the user is using Notifications. The Activity might as well go on the backstack in the meantime, you don't care. Note that in this case, the service is serving the Activity which started it, and there is no requirement for it to be available generically to other Activities.

On the other hand, bound service is a more generic service or one that needs to serve multiple activities, and more over, needs multiple bidirectional interactions i.e. Activity sends a message to Service, then Service sends a message back to Activity and so on. Consider the example of background music player service, where you pass the music file or remote stream URI/URL to the service, then another activity could change the volume or switch to another track etc. A message back from service to activity could be that the mp3 file is incomplete or corrupted, or a track completed message.

In fact, I came to this question looking for the answer to this exact question, but found the answer quite satisfactorily and complete in the link provided by @SagarPikhwal. Admittedly, I'm a newbie as far as Android programming is concerned, so the above is all as per what I understood !

Edit: Realized that I didn't answer (to the best of my abilities), the other part of the question about the code you saw for Google Camera. I think the reason why they create a bound service is because Camera is a common shared resource, and there could be multiple users of that system resource simultaneously. The activity using the camera service to capture an image or video is not the exclusive user. The Google Camera application is yet another user of the camera hardware, while there could others, and all of them served by the bound-service.

Community
  • 1
  • 1
bdutta74
  • 2,798
  • 3
  • 31
  • 54
  • Thanks for your answer, but it still covers only started services and started-and-bound services. My question was about just bound services. Your example of music service is very good, I agree, but still such service will (and should) work without any `Activity` alive, so it is not a pure bound service (which stays alive only while bound activity is alive). – Dmitry Zaytsev Sep 07 '14 at 11:55
  • Regarding camera - as per documentation, `Activity` is an exclusive owner of `Camera`, so no - it is not the case that they're holding it in `Service`. What they do is saving of captured data. And once again - it seems strange to me, as such `Service` won't stay alive after `CameraActivity` is destroyed. – Dmitry Zaytsev Sep 07 '14 at 11:57
  • Ah, I see - my knowledge of Android is clearly quite limited so far. I did read (and interpret as such) about Bluetooth service being pure bound service, in the sense that no user-land application activity starts it. Any activity or broadcast-receiver needing to use Bluetooth must bind to this service and use the message based mechanism to invoke functionality of discovery, pairing/authentication, data communication etc. – bdutta74 Sep 07 '14 at 13:36
  • I didn't read about Bluetooth service, but I assuming that it is running in a separate process. In that case bound service is indeed the very convenient way to communicate with it. Although, I mentioned in my question `... within the application process`, so that is also out of the scope :) – Dmitry Zaytsev Sep 07 '14 at 14:54
  • By the way, if you read that far I wouldn't call your knowledge limited. It is more than ok! – Dmitry Zaytsev Sep 07 '14 at 14:56
  • Thanks for your kind words. Only after your last comment did I revisit some of the articles and I realized that I had missed the point about "local" service (as in part of same process, separate thread or not) and "remote" service (as in part of a different unix process). Back to your question, don't you think that a service that is "local" and implemented as a bound service, could be used as a "remote" bound service by few other apps ? Not sure how real this is, but say something like a chat client that runs the XMPP client as a service... – bdutta74 Sep 07 '14 at 16:42
  • ... and permits other application (who could use the API of this messaging service) to communicate to the XMPP client service as a "remote" unbound service to post/receive messages ? Say, something like twitter client or Whatsapp client ? This way, every app that wishes to use the messaging service doesn't have to implement it's own client service. – bdutta74 Sep 07 '14 at 16:44