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(R.id.tvLatitude);

longitude = (TextView) findViewById(R.id.tvLongitude);

altitude = (TextView) findViewById(R.id.tvAltitude);

accuracy = (TextView) findViewById(R.id.tvAccuracy);

speed = (TextView) findViewById(R.id.tvSpeed);

sensorType = (TextView) findViewById(R.id.tvSensor);

updatesOnOff = (TextView) findViewById(R.id.tvUpdates);

switchGpsBalanced = (ToggleButton) findViewById(R.id.tbGps_Balanced);

locationOnOff = (ToggleButton) findViewById(R.id.tvLocationOnOff);


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() {

   @Override

   public void onClick(View v) {

      if (switchGpsBalanced.isChecked()){

         //using GPS only

         sensorType.setText("GPS");

      } else {

         //using balanced power accuracy

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

      }


   }

});


locationOnOff.setOnClickListener(new View.OnClickListener() {

   @Override

   public void onClick(View v) {

      if (locationOnOff.isChecked()) {

         //location updates on

         updatesOnOff.setText("On");

      } else {

         //location updates off

         updatesOnOff.setText("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 MainActivity.java 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