North Border Software




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

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:

dependencies {

    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'com.android.support:appcompat-v7:26.1.0'

    implementation 'com.android.support.constraint:constraint-layout:1.0.2'

    testImplementation 'junit:junit:4.12'

    androidTestImplementation 'com.android.support.test:runner:1.0.1'

    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'

}


Add the following line and ‘Re-Sync’ the project:


dependencies {

    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'com.android.support:appcompat-v7:26.1.0'

    implementation 'com.android.support.constraint:constraint-layout:1.0.2'

    testImplementation 'junit:junit:4.12'

    androidTestImplementation 'com.android.support.test:runner:1.0.1'

    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'

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

}


After this tutorial you build.gradle file should look like the one below:




The next task is to add the permissions to the Android Manifest file. Open the Android Manifest file and add the following line:


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


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:


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


Contraint Layout FusedLocationProviderAPI Deprecated


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:



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