Android Basics : Learn how to track Location changes by adding a LocationListener

In order to use the Location Service you need to do the following:

  • Implement LocationListener
LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
        if (lastLocation == null) {
            lastLocation = location;
        distanceInMetres += location.distanceTo(lastLocation);

    public void onStatusChanged(String provider, int status, Bundle extras) {


    public void onProviderEnabled(String provider) {


    public void onProviderDisabled(String provider) {

  • Create LocationManager using the Listener to start tracking user location changes
LocationManager locationManager = (LocationManager)
if (ActivityCompat.checkSelfPermission(this,
        Manifest.permission.ACCESS_FINE_LOCATION) != 
        && ActivityCompat.checkSelfPermission(this,
        Manifest.permission.ACCESS_COARSE_LOCATION) != 
            PackageManager.PERMISSION_GRANTED) {
  • Add Permissions to the Manifest file.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

The Source Code for this implementation is available here

Author: Ankur

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: Logo

You are commenting using your 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 )

Connecting to %s

%d bloggers like this: