Thursday, September 8, 2016

What is the difference between Started Service and Bound Service in Android?

Very frequent interview question , What is the difference between Started Service and Bound Service?

Ans: Let's first understand what is started Service and Bound Service?.

Started Service: When you wants to perform a long running task in background means without UI(User Interface) work such as sending data to server ,performing network operations such as get and post,Playing Musics etc. then you can use started service So,question is why not Bound Service?.Lets leave bound service at the moment.But let me clear here only that It is not advisable to use bound service here,it is not a best practice in Android.Lets look at the call backs of started Service.

Lets start Service you will do something like this in the Activity:
startService(new Intent(this, FirstService.class));
Now lets watch the call backs of it:


It is clearly visible that it will stop itself once the task is done or would be stopped by the client.There is nothing extra callbacks there to update anything else.What i mean to say is:    The service then runs indefinitely and must stop itself by calling stopSelf().So,When a service is started, it   has a life-cycle that's independent of the component that started it and the service can run in the background definitely, even if the component that started it is destroyed.  So,if you want to perform a long running task which is related with the UI then how will you update the UI, When the component that started it is being destroyed by the Android Operating System.There bound services comes into the picture.                                                                                                                                                       Now , Lets see what is Bound Service and if you can see in the picture, there  are some extra call back methods are specially in bound services which itself explains many thing.                                                                                                                                                                               
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 inter-process communication (IPC).           A bound service runs only as long as another application component is bound to it. Multiple components can bind to the service at once, but when all of them unbind, the service is destroyed.

You should create a bound service when you want to interact with the service from activities and other components in your application or to expose some of your application's functionality to other applications, through inter-process communication (IPC).                                                                       So,it clear that when you want to do some task which is related with UI or you want to create some thing which other components can use of your application then in that case you can use bind service instead of using started service.So this is the major difference between started and bound services.

  
                                                              






No comments: