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.

BoundService

 

Author: androidsmartly

I am a Tech Enthusiast who likes to work on new Ideas. Sharing knowledge on Android, Marketing, Startups and More.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s