Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

Thanks

like image 208
mohan Avatar asked Aug 11 '14 09:08

mohan


People also ask

What is the difference unbound service bound service and intent Service How do you use eg start and stop them?

Intent Service gets starts by calling startService(). Unbound Service is stopped or destroyed explicitly by calling stopService(). bounded Service is unbind or destroyed by calling unbindService(). Unbound Service is independent of the component in which it is started.

What is a bound service in 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 difference between started and bound services in Android?

Started services run until they are stopped or destroyed and do not inherently provide a mechanism for interaction or data exchange with other components. Bound services, on the other hand, provide a communication interface to other client components and generally run until the last client unbinds from the service.

What is the difference between service and IntentService in Android?

Service class uses the application's main thread, while IntentService creates a worker thread and uses that thread to run the service. IntentService creates a queue that passes one intent at a time to onHandleIntent() . Thus, implementing a multi-thread should be made by extending Service class directly.

What is the difference between bound and unbound services in Java?

The Unbound service runs in the background indefinitely. Where As The Bound service does not run in the background indefinitely. 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.

What is a bound service in Android?

This type of android service allows the components of the application like activity to bound themselves with it. Bound services perform their task as long as any application component is bound to it. More than one component is allowed to bind themselves with a service at a time.

How do you start a service in unbounded mode?

Started Service (Unbounded Service): By following this path, a service will initiate when an application component calls the startService () method. Once initiated, the service can run continuously in the background even if the component is destroyed which was responsible for the start of the service.

What is the difference between started service and bound service?

A bound service allows extended two way communication between the activity and the service whereas a started service need not return any results to the client activity A bound service will service multiple clients (as long as there is at least one client bound to it) while a started service performs a single operation and then shuts down.


4 Answers

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

like image 81
Spurdow Avatar answered Sep 21 '22 04:09

Spurdow


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/

like image 26
Saubhagya Ranjan Das Avatar answered Sep 19 '22 04:09

Saubhagya Ranjan Das


  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

like image 30
Chinki Sai Avatar answered Sep 22 '22 04:09

Chinki Sai


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

  • Application component calls stopService() method
  • Service itself calls SelfStop() method.

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

like image 27
Rohit Singh Avatar answered Sep 21 '22 04:09

Rohit Singh