How to receive location updates

If your application needs frequent location updates, your application can request periodic updates. This recipe will demonstrate this using the requestLocationUpdates() method from GoogleApiClient.

Getting ready

Create a new project in Android Studio and call it: LocationUpdates. Use the default Phone & Tablet options and select Empty Activity when prompted for Activity Type.

How to do it...

Since we are receiving updates from the system, we won't need a button for this recipe. Our layout will consist of just the TextView to see the location data. Open the Android Manifest and follow these steps:

  1. Add the following permission:
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
  2. Open the file build.gradle ...

Get Android Application Development Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.