Calling the Bound Service from Main Activity

In the last Article you learnt how to bind MainActivity with BoundService.

Time for you to call the displayDistance method to display the distance traveled. You can create a new method which will return the distance computed. This method will create a Handler and invoke the Service every second.

private void displayDistance() {
    final Handler handler = new Handler(); Runnable() {
        public void run() {
            double distance = 0;
            if(mDistanceTraveledService != null){
                distance = mDistanceTraveledService.getDistanceTraveled();
            handler.postDelayed(this, 1000);

Next add a TextView in the Layout XML file to display this.


Full source code is available here


Android Basics : Learn how to bind a Bound Service to an Activity using ServiceConnection

In the previous Article you saw how to add the LocationListener to the Service class. You created a method getDistanceTraveled to return the distance.

Next step is to call this method from the MainActivity. So how to do this ?

You need to create a ServiceConnection object and implement its methods.

ServiceConnection mServiceConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName name, IBinder service) {
        DistanceTraveledService.DistanceTravelBinder distanceTravelBinder = 
        mDistanceTraveledService = distanceTravelBinder.getBinder();
        bound = true;

    public void onServiceDisconnected(ComponentName name) {
        bound = false;

Once you have the ServiceConnection object you need to create an intent and call bindService method.

protected void onStart(){
    Intent intent = new Intent(this, DistanceTraveledService.class);
    bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);

Source code is available here

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.

Android Basics – Learn how to use Bound Services

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:

  1. Create a Binder class which will connect the Service class with MainActivity.
  2. Create a LocationListener which will track location changes. Register this with DistanceTraveledService class.
  3. 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.



Android Basics – Introduction to Services

A Service helps you to execute tasks in the background. Like downloading a file or tracking user location changes.

It is an Application Componenet with no user interface. It has a simple life cycle and comes with a set of features.

There are two types of Services

Bound Service  This kind of service is bound to a component like an Activity and runs as long as the Activity runs.

Started Service  This kind of service can run indefinitely. Even when the Activity that started is is destroyed.

You can create a new service by extending either the Service Class or the IntentService Class.

IntentService allows you to create a started service.

In the video you will learn how to get started by creating an IntentService.

Source Code is available here