Enter your keyword

Receiving Android Location Updates

Receiving Android Location Updates

Receiving Android Location Updates

Is your app heavily dependent on geodata, for example if your app is tracking user locations, suggesting best routes to users for say driving jogging, or even if you app just keep track of locations of assets that you late use, it needs to get the location of the device at regular intervals.

You’re able to request periodic updates Android fused location provider. The API updates your app periodically with the best available location, based on the currently-available location providers such as WiFi and GPS (Global Positioning System). The accuracy of the location is determined by the providers, the location permissions you’ve requested, and the options you set in the location request.

In this tutorial, I will shows you how to request regular updates about a device’s location using the requestLocationUpdates() method in the fused location provider.

The fused location provider is one of the location APIs in Google Play services. It manages the underlying location technology and provides a simple API so that you can specify requirements at a high level, like high accuracy or low power. It also optimizes the device’s use of battery power.

Dependencies

You need to include the Google Play Services Location library to be able to access the location services. The maps library is optional since you done need to access the location updates, but in our case we shall be showing the updates on a map thus we include as well.

Permissions

Location permissions

If your app needs to access the user’s location, you must request permission by adding the relevant Android location permission to your app.

Android offers two location permissions: ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION. The permission you choose determines the accuracy of the location returned by the API. You only need to request oneof the Android location permissions, depending on the level of accuracy you need:

  • android.permission.ACCESS_COARSE_LOCATION – Allows the API to use WiFi or mobile cell data (or both) to determine the device’s location. The API returns the location with an accuracy approximately equivalent to a city block.
  • android.permission.ACCESS_FINE_LOCATION – Allows the API to determine as precise a location as possible from the available location providers, including the Global Positioning System (GPS) as well as WiFi and mobile cell data.

External storage permission

If you’re targeting version 8.3 or later of the Google Play services SDK, you no longer need the WRITE_EXTERNAL_STORAGE permission to use the Google Maps Android API. If you’re targeting earlier versions of the Google Play services SDK, you must request theandroid.permission.WRITE_EXTERNAL_STORAGE permission.

Note: If your app is targeting API level 23 (Android 6.0), which requires the use of runtime permissions, you shouldtarget version 8.3 or later of the Google Play services SDK.

Permissions automatically merged into your manifest

The following permissions are defined in the Google Play services manifest, and are automatically merged into your app’s manifest at build time. You don’t need to add them explicitly to your manifest:

Google Api Client

The main entry point for Google Play services integration. Before any operation is executed, the GoogleApiClient must be connected. It has the following nested classes…

class GoogleApiClient.Builder Builder to configure a GoogleApiClient.
interface GoogleApiClient.ConnectionCallbacks Provides callbacks that are called when the client is connected or disconnected from the service.
interface GoogleApiClient.OnConnectionFailedListener Provides callbacks for scenarios that result in a failed attempt to connect the client to the service.

Location Listener

Implement the LocationListener interface and define theLocationListener.onLocationChanged() callback method. The fused location provider invokes this method.

……

….

 

Get the Last Known Location

You want to start off getting the last known location of your device before you start receiving periodic location updates.  Using Google Play Services location APIs, your app can request the last known location of the user’s device.

Request Location Updates

In most cases, you are interested in the user’s current location, which is usually equivalent to the last known location of the device.

Define the Location Update Callback

Stop Location Updates

Update UI to show ‘new’ current location