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.

Author: codesmartly

I am Enthusiastic about Learning new things and sharing my Knowledge. I like programming and have a pretty good background in Computer Science.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s