36

Can anybody explain what is difference between unbound and bound service in android and explain about intent service

Thanks

Muntasir
  • 798
  • 1
  • 14
  • 24
mohan
  • 13,035
  • 29
  • 108
  • 178

4 Answers4

68

Bound Service

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

When the last client unbinds from the service, the system destroys the service EXCEPT If the service was started by startService

Unbound Service or 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.

BUT

Most confusion about the Service class actually revolves around what it is not:

A Service is not a separate process. The Service object itself does not imply it is running in its own process; unless otherwise specified, it runs in the same process as the application it is part of.

A Service is not a thread. It is not a means itself to do work off of the main thread (to avoid Application Not Responding errors).

That is where IntentService are used.

IntentService is a subclass of Service that uses a worker thread to

handle all start asynchronous requests (expressed as Intents) on demand, one at a time. Clients send requests through startService(Intent) calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work.

Example

hope it helps :)

Siguza
  • 21,155
  • 6
  • 52
  • 89
Spurdow
  • 2,025
  • 18
  • 22
  • 2
    Clear definitions of each type thank you! I'm just looking into services and trying to find a answer to a question related to this. An Intent Service does not handle requests in parallel, it uses one background thread and queues requests. Could a Bound service, with its own thread pool be a better approach for scalability? – Mark Dec 16 '15 at 18:08
  • 2
    You should use a service instead and yes with its own thread pool would be a better approach and choose newFixedThreadPool with a fixed number of cpu(depending on available cpu). I have this approach using mqtt actually and its working gracefully. – Spurdow Dec 17 '15 at 12:43
  • 2
    Thanks for the reply, and advice for using a service - just reading up about mqtt and push notifications .. As soon as I learn about one thing, it invariably leads to a lot of other topics! – Mark Dec 18 '15 at 10:39
11

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. a tabular difference is given in below link which is very useful for interviews http://infobloggall.com/2014/04/15/bounded-service-in-android/

8
  1. Unbound service is started when component (like activity) calls startService() method Where As A service is bound when another component (e.g. client) calls bindService() method.

  2. The Unbound service can stop itself by calling the stopSelf() method. Where As The Bound service cannot be stopped until all clients unbind the service.

  3. The Unbound service runs in the background indefinitely. Where As The Bound service does not run in the background indefinitely.

  4. The Unbound service is stopped by stopService() method. Where As In The Bound service, The client can unbind the service by calling the unbindService() method.

Thanks

Chinki Sai
  • 251
  • 4
  • 5
  • 1
    +1 from me for actually providing a good answer to an old question. Now add a link to the docs at the bottom and I'd consider this a very good answer. – m02ph3u5 May 19 '17 at 11:26
8

Bound and Unbound Services are not two sides of a coin

A service can be a bound or unbound(started) or both, It is just the matter of implementation you provide to the callback methods of Service class. See all four callback methods here

But for the sake of differentiation here you go

1. Staring a service

Unbound Service is started by calling startService() method.
Bound Service is started by calling bindService() method.
However in both calls system calls onStartCommand() method internally

2. Life Span of a service

Once an unboundService is started it runs indefinitely until

BoundService runs as long as the service is bound to a client. When there is no active client bound with the service, the system destroys the Service

3. onBind() method

When you are writing a service you will have to override the onBind(). If
Unbound Service then return null
BoundService then return IBinder object.

Though unbound services does not return Ibinder object it does not mean that it can not interact with application component. There are ways to do that for example BroadCastReceiver or ResultReceiver

One way vs Two-way communication with Service

When you want two-way communication with your Service then you should bind your service with Activity.
Eg. Playing music in the background with pause, play option (Activtiy <-> Service).

Go with unbound or started service when you just want your Service to update your Activity (Service->Activity).
Eg: Timer Service which updates Activity every second.

Another example

You have written some Service which deals with Location changes.
If you want to update your activity when you move 10 meters (Go with unbound service).
If you want to see the coordinates of your current location when you click some button in the activity. (Go with the bound service).

Community
  • 1
  • 1
Rohit Singh
  • 16,950
  • 7
  • 90
  • 88