Can anybody explain what is difference between unbound and bound service in android and explain about intent service
Thanks
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.
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).
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.
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.
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.
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.
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.
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.
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.
hope it helps :)
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/
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.
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.
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.
Thanks
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
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
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
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
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With