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

The Android FusedLocationProviderApi was deprecated toward the end of 2017. The advice is to switch to the connectionless FusedLocationProviderClient. This tutorial shows you how to use the FusedLocationProviderClient to receive location updates to your Android apps. This tutorial is written in Java and a Kotlin version will be available soon.

First create a new project. If you need to understand how to create a project, then look at my tutorial here. For this tutorial, I selected an empty layout to avoid any unnecessary clutter in my code.

After creating a project, it is important to set up the dependencies in the build.gradle file. We need to add Google Play Services to our project to access all the location-based Java Classes and Methods. Do not add the full suite of Google Play Services. This will lead to excessive code and may generate Multidex errors. Only select the location-based API. This can be found at the following website: https://developers.google.com/android/guides/setup. Select the following API, but bear in mind you will need to access the latest version that may be higher than 11.8.0:

com.google.android.gms:play-services-location:11.8.0

Open the build.gradle file in the app folder. You will find a list of dependencies like those below:

After this project your Android Manifest file should look like the one below:

We only need add permission for the fine location. Adding permission for the fine location automatically implies that permission for the coarse location will be enabled. Even though we will look at using WiFi to get the location of the device, we do not need to enable permission to access the Internet. Permission for the fine location is all that is needed.

The next task is to create a layout. For this tutorial, we will use a Constraint Layout. If you need to understand Constraint Layouts, then look at my tutorial here.

The project app will show the following:

Longitude

Latitude

Altitude

Accuracy of the location

Speed

Sensor Type (GPS or Cell Tower/Wifi)

Whether location updated are on or off

The sensor type and location updates on/off will be controlled by ToggleButtons. The image below indicates how the constraint are arranged.

A screenshot of the completed layout is below.

Your completed layout should look like the one below:

We have not worked on the Java MainActivity in this part. We will start to code in part 2 where we look to create a Java framework for the project. After this tutorial your MainActivity.Java file should look like to one below: