Home Tutorials Hints & Tips

Android Location Based Services using the GoogleAPIClient and the FusedLocationProvider API - Part 3

In this part 3 of the series looking at Android Location based services, we will finally get to complete the coding and test the project. In fact, there is very little coding left to do following on the Java framework constructed in the last tutorial.

Since location based services are power hungry, it is good practice to pay attention to the Android activity lifecycle and write our code using appropriate override methods. A diagram of the activity lifecycle is shown below.

The Android Activity Lifecycle

You can see that after our activity is created the onStart() method called. This is a logical place to connect our GoogleAPIClient. Once the activity has started, the onResume() method is called. This too is called after the activity is paused. Therefore, we need to add some code to these. We would want to stop location updates once the activity is paused and start them again once the activity is resumed. Finally, onStop() is called when we want to stop the activity. This is a logical place to disconnect our GoogleAPIClient.

Therefore, start by overriding the onStart(), onResume(), onPause() and onStop() methods.

@Override
protected void onStart() {
   super.onStart();
}

@Override
protected void onResume() {
   super.onResume();
}

@Override
protected void onPause() {
    super.onPause();
}

@Override
protected void onStop() {
    super.onStop();
}

The single line of code that need to be added to the onStart() method is straightforward and it connects the GoogleAPIClient.

googleApiClient.connect();

If using Android Studio, you may be prompted to add the following permissions to the Android Manifest file. If not, then add them.

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

The fine location permits use of the GPS and the course location other means such as the WiFi of phone masts.

After the onStart() method has executed and the GoogleAPIClient has connected the onConnected() callback method is called. Within this callback method, we must add a line of code to start the location updates. We will actually call the same code in the onResume() method so the best approach is to let Android Studio create a private method with the line of code below:

requestLocationUpdates();

A single line of code in the requestLocationUpdates() method starts the location updates using the Fused Location ProviderAPI:

LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);

If you are running Google Android API 23 (Marshmallow) or higher you will need to add code to ensure that the user actively authorizes permissions to use location service irrespective of whether permission is granted in the Android Manifest file. For now, add the default code suggested by Android Studio to clear the error. Handling permissions in run time will be addressed in a later tutorial.

onResume() is called after the onStart() method and also after onPause(). We want to add the following code to the onResume() method;

if (googleApiClient.isConnected()) {
   requestLocationUpdates();

Basically, all we are saying is that if the GoogleAPIClient is connected then start to location updates. If it is not for any reason, then do nothing. We need to call the requestLocationUpdates() method because we will suspend location updates when onPause() is called. The line of code we need to add to the onPause() is shown below:

LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, this);

The key text here is removeLocationUpdates, that suspends location updates.

There are two other call-back methods for which we have not added any code.

@Override
   public void onConnectionSuspended(int i) {
}

@Override
   public void onConnectionFailed(ConnectionResult connectionResult) {
}

For this example, we will leave them as default. You may want to consider adding some error logging code in these should the connection fail.

We now need to add code to the onStop() method. In this method we simply want to disconnect the nGoogleAPIClient. Add the line of code below to the onStop() method:

googleApiClient.disconnect();

Our final task is to update the TextViews with the location of the device. Start by declaring two double floating point variables  for the longitude and latitude:

private Double myLatitude;
private Double myLongitude;

We then need to add the following code to the onLocationChanged() method.

@Override
public void onLocationChanged(Location location) {
   myLatitude = location.getLatitude();
   myLongitude = location.getLongitude();
   latitudeText.setText("Latitude : " + String.valueOf(myLatitude));
   longitudeText.setText("Longitude : " + String.valueOf(myLongitude));
}

This is all that is needed and we are now ready to test the project. This is best done on a real device by creating an APK file.

The screenshot below shows and example for our app.

Android Location Based Services Layout Example

After this tutorial your MainActivity.java file should look similar to the one below:

Your Android Manifest file should look similar to the one below:


Download Download Location Project Files



Privacy and Cookies Disclaimer Copyright
© 2015 - 2018 North Border Tech Training All rights reserved