Tracking Movement

Most location-sensitive applications will need to be reactive to user movement. Simply polling the Location Manager will not force it to get new updates from the Location Providers.

Where Am P

Your Current Position is: Lat:51.438137000000005 Long:-0.236848


Use the requestLocationUpdates method to get updates whenever the current location changes, using a LocationListener. Location Listeners also contain hooks for changes in a provider's status and availability.

The requestLocationUpdates method accepts either a specific Location Provider name or a set of Criteria to determine the provider to use.

To optimize efficiency and minimize cost and power use, you can also specify the minimum time and the minimum distance between location change updates.

Listing 8-2 shows the skeleton code for requesting regular updates based on a minimum time and distance.

LISTING 8-2: Requesting location updates

Available for download on

String provider = LocationManager.GPS_PROVIDER;

int t = 5000; // milliseconds int distance =5; // meters

LocationListener myLocationListener = new LocationListener() {

public void onLocationChanged(Location location) { // Update application based on new location.

public void onProviderDisabled(String provider){ // Update application if provider disabled.

public void onProviderEnabled(String provider){ // Update application if provider enabled.

public void onStatusChanged(String provider, int status,

Bundle extras){ // Update application if provider hardware status changed.

locationManager.requestLocationUpdates(provider, t, distance, myLocationListener);

When the minimum time and distance values are exceeded, the attached Location Listener will execute its onLocationChanged event.

Prepared for ASHLEE KABAT, email: [email protected] Order number: 56760408 This PDF is for the purchaser's personal use in accordance with the Wrox Terms of Service and under US copyright as stated on this book's copyright page. If you did not purchase this copy, please visit to purchase your own copy.

You can request multiple location updates pointing to different Location Listeners and using different minimum thresholds. A common design pattern is to create a single listener for your application that broadcasts Intents to notify other components of location changes. This centralizes your listeners and ensures that the Location Provider hardware is used as efficiently as possible.

To stop location updates, call removeUpdates, as shown in the following code. Pass in the Location Listener instance you no longer want to have triggered.


Most GPS hardware incurs significant power cost. To minimize this you should disable updates whenever possible in your application, especially when your application isn't visible and location changes are being used to update an Activity's user interface. You can improve performance further by making the minimum time between updates as long as possible.

Privacy is also a factor when your application tracks the user location. Ensure that your application is using the device location data in a way that respects the user's privacy by:

> Only tracking location when necessary for your application

> Notifying users of when you are tracking their locations, and how that location information is being used and stored

> Allowing users to disable location updates, and respecting the system settings for LBS preferences.

Was this article helpful?

0 0
Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide

Post a comment