Saturday 7 May 2016

Android : Service Example(Started Service, Bind Service, Bind Service Using AIDL, IntentService with Broadcast, IntentService with ResultReceiver)


Adding sample App to demonstrate different types of Services in Android.

Below example demonstrate following types of services in Android.


  • Started Service
  • Bound Service(In Same App)
  • Bound Service(Across The Application Using AIDL aka InterProcessCommunication)
  • IntentService(Return Data Using BroadcastReceiver)
  • IntentService(Return Data Using ResultReceiver) 

StartedService:
  • A service is started when any App component(Activity in our case) starts it by calling startService().
  • Started services cannot return results/values or interact with its starting component.
  • If we want to execute long running task in started service we need to create new Thread inside it otherwise app can face crashes or may hang UI as long as task is executing.
  • Started service can run indefinitely in background even if the component that started(Activity-In our case) it is destroyed.
USE : This type of service can be use to download/upload data in background which can be use later in our application like download images which can be displayed on ListView or any other UI component.


Bound Service(In Same App):
  • A service is "bound" when an application component binds to it by calling bindService().
  • This type of service can be used when Staring component(Activity-In our case) want to interact with service.
  • A Bound service provides 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.
  • Bound service can be used in multi threaded environment it can handle multiple request simultaneously.  
USE : This type of service can be used when we want to communicate with service from application component(like Activity) for example we can upload data in background using service and we can call its public function to check uploading status and display it on UI.


Bound Service(AIDL):
  • This is also a bound service the main difference is in our implementation.... in above case we are calling same app service from same app component(Activity) and in case of AIDL service we can call one App's service from other App's component(Activity).
  • We can communicate across the application(InterProcessCommunication) using AIDL.
  • If we want to make our service private so that other application can not bind to our service we can put android:exported="false" in manifest in service node.

USE : AIDL service can be use to create Utility app where we can provide some common functionalities which can be access by different applications.


IntentService:
  • IntentService is subclass of Service class. IntentService runs on Worker-Thread so we dont have to create new Thread inside it to execute long running task like Service.
  • IntentService maintain queue of request if any request is executing and you send another request this request will wait till the first request is finish.
  • IntentService is not useful when you are working in multi-threaded environment.
  • There is no direct way to interact with UI component from IntentService but we can implement BroadcastReceiver and ResultReceiver for communicating with Activity.
USE : IntentService can be used to execute some task in background and at the same time we can get its result on Activity using BroadcastReceiver and ResultReceiver.

                           
                                                     Service Demo App Screenshot


AIDL Server App Screenshot


For more detail please download App source code from below URL:


You can download demo code from below URL:

Service Demonstration App URL:

AIDL Server App URL: