So far you have learnt about the Started Service, which runs indefinitely till it completes.
A Bound Service is attached to the component which invokes it. If an Activity invokes this service then the Bound Service will depend on the LifeCycle of the Activity.
An example can be if you are using location based service.
To understand this you are going to create a new Service class – DistanceTraveledService.
This Service class will track the distance the user travels. After calculating the distance it will return the value to the MainActivity class.
Let’s look at the steps needed:
- Create a Binder class which will connect the Service class with MainActivity.
- Create a LocationListener which will track location changes. Register this with DistanceTraveledService class.
- Create a public method getDistance() in the DistanceTraveledService. This method will be called by the MainActivity.
The following diagram shows the components needed. In the next Article you will start making the changes.