Can anybody explain what is difference between unbound and bound service in android and explain about intent service
Thanks
Can anybody explain what is difference between unbound and bound service in android and explain about intent service
Thanks
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).