Android FusedLocationProviderAPI Deprecated - How to use the FusedLocationProviderClient - Part 2

North Border Software

Home Tutorials Hints & Tips About
Share on Facebook Share on Twitter

Android FusedLocationProviderAPI Deprecated - How to use the FusedLocationProviderClient - Part 2

This series of tutorials shows you how to use the FusedLocationProviderClient in place of the deprecated FusedLocationProviderAPI to receive location updates to your Android device. This tutorial is written in Java and a Kotlin version will be available soon.

In part 1 we set up a layout using a Constraint Layout, set up permissions in the Manifest and placed the Google Play Services dependency in the Gradle file. In this part we simply create some Java members for the TextViews and ToggleButtons and associate them with the views in the layout.

The Java members are listed below:

private TextView latitude;

private TextView longitude;

private TextView altitude;

private TextView accuracy;

private TextView speed;

private TextView sensorType;

private TextView updatesOnOff;

private ToggleButton switchGpsBalanced;

private ToggleButton locationOnOff;

Next use the findViewById method to associate the members with the id of the views in the layout file:

latitude = (TextView) findViewById(;

longitude = (TextView) findViewById(;

altitude = (TextView) findViewById(;

accuracy = (TextView) findViewById(;

speed = (TextView) findViewById(;

sensorType = (TextView) findViewById(;

updatesOnOff = (TextView) findViewById(;

switchGpsBalanced = (ToggleButton) findViewById(;

locationOnOff = (ToggleButton) findViewById(;

Next, we will create onClickListeners for the ToggleButtons and set the text to indicate whether the location updates are on or off and whether we will be using GPS or Cell Tower and WiFi to obtain the location. The latter is what we call balanced power accuracy.

switchGpsBalanced.setOnClickListener(new View.OnClickListener() {


   public void onClick(View v) {

      if (switchGpsBalanced.isChecked()){

         //using GPS only


      } else {

         //using balanced power accuracy

         sensorType.setText("Cell Tower and WiFi");




locationOnOff.setOnClickListener(new View.OnClickListener() {


   public void onClick(View v) {

      if (locationOnOff.isChecked()) {

         //location updates on


      } else {

         //location updates off





So, what we have achieved here is to create a Java framework for our project such that in the next part we can focus on the location code. In part 3 we will look at getting the last known location of the device and deal with runtime permissions.

After this tutorial your should look like the one below:

Download Download project files

© 2015 North Border Software All rights reserved
Privacy and Cookies Terms of Use Disclaimer Copyright
North Border Software Logo

Android Apps and Training