Android Basics – Learn how to bind an Activity to a Bound Service

In the last Article you saw how to create a new Bound Service. The next step is to bind this Service to the Activity. In order to do this you will have to create a new Inner Class which extends Binder class. Add this in the DistanceTraveledService class that you created.

public class DistanceTravelBinder extends Binder{
    DistanceTravelBinder getBinder(){
        return DistanceTravelBinder.this;

The purpose of this inner class will be to return an object of the Service class. When you create a Bound Service you need to create a Binder implementation. The Activity will use this to get a reference to the Service.

DistanceTravelBinder mDistanceTravelBinder = new DistanceTravelBinder();

Source Code is available here. In the next article you will learn how to use the Service class to fetch distance and return to Activity.