Understanding the Location Manager Service

The LocationManager service is one of the key services offered by the android.location package. This service provides two things: a mechanism for you to obtain the device's geographical location, and a facility for you to be notified (via an intent) when the device enters a specified geographical location.

In this section, you are going to learn how the LocationManager service works. To use the service, you must first obtain a reference to it. Listing 7-18 shows the usage pattern for the LocationManager service.

Listing 7-18. Using the LocationManager Service import java.util.List;

import android.app.Activity; import android.content.Context; import android.location.Location; import android.location.LocationManager; import android.os.Bundle;

public class LocationManagerDemoActivity extends Activity {

^Override protected void onCreate(Bundle savedlnstanceState) {


LocationManager locMgr = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE); Location loc = locMgr.getLastKnownLocation(LocationManager.GPS_PROVIDER);

List<String> providerList = locMgr.getAllProviders(); }

The LocationManager service is a system-level service. System-level services are services that you obtain from the context using the service name; you don't instantiate them directly. The android.app.Activity class provides a utility method called getSystemService() that you can use to obtain a system-level service. As shown in Listing 7-18, you call getSystemService() and pass in the name of the service you want—in this case, Context.LOCATION_SERVICE.

The LocationManager service provides geographical-location details by using location providers. Currently, there are two types of location providers: GPS and Network. GPS providers use a Global Positioning System to obtain location information, whereas network providers use cell-phone towers or WiFi networks to obtain location information. The LocationManager class can provide the device's last-known location (practically the current location) via the getLastKnownLocation() method. Location information is obtained from a provider, so the method takes as a parameter the name of the provider you want to use. Valid values for provider names are LocationManager.GPS_PROVIDER and LocationManager.Network. Calling getLastKnownLocation() returns an android.location.Location instance. The Location class provides the location's latitude and longitude, the time the location was computed, and possibly the device's altitude, speed, and bearing.

Because the LocationManager operates on providers, the class provides APIs to obtain providers. For example, you can get all of the providers by calling getAllProviders(). You can obtain a specific provider by calling getProvider(), passing the name of the provider as an argument (such as LocationManager.GPS_PROVIDER).

To that end, the gotcha with using the LocationManager services occurs at development time—LocationManager needs location information and the emulator doesn't really have access to GPS or cell towers. So in order for you to develop with the LocationManager service, you (sort of) tell the emulator about your location. For example, you can ask the LocationManager to notify you if the device is near a location. To test something like this with the emulator, you would have to send the emulator periodic updates on your location; the emulator would then play that information back to the application. Listing 7-19 shows an example.

Listing 7-19. Registering for Location Updates import android.app.Activity; import android.content.Context; import android.location.Location; import android.location.LocationListener;


import android.location.LocationManager; import android.os.Bundle; import android.widget.Toast;

public class LocationUpdateDemoActivity extends Activity {

^Override public void onCreate(Bundle savedInstanceState) {


LocationManager locMgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

LocationListener locListener = new LocationListener() {

public void onLocationChanged(Location location) {

Toast.makeText(getBaseContext(), "New location latitude [" +

location.getLatitude() +

"] longitude [" + location.getLongitude()+"]", Toast.LENGTH_SHORT).show();

public void onProviderDisabled(String provider) {

public void onProviderEnabled(String provider) {

public void onStatusChanged(String provider, int status, Bundle extras) {


locMgr.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, locListener);

As we said, one of the primary uses of the LocationManager service is to receive notifications of the device's location. Listing 7-19 demonstrates how you can register a listener to receive location-update events. To register a listener, you call the requestLocationUpdates() method, passing the provider type as one of the parameters. When the location changes, the LocationManager calls the onLocationChanged() method of the listener with the new Location. In Listing 7-19, our listener implementation simply shows a message in the UI to indicate the new latitude and longitude of the location. To test this in the emulator, you can use the Dal-vik Debug Monitor Service (DDMS) interface that ships with the ADT plug-in for Eclipse. The DDMS UI provides a screen for you to send the emulator a new location (see Figure 7-11).

Figure 7-11. Using the DDMS UI in Eclipse to send location data to the emulator

As shown in Figure 7-11, the Manual tab in the DDMS user interface allows you to send a new GPS location (latitude/longitude pair) to the emulator. Sending a new location will fire the onLocationChanged() method on the listener, which will result in a message to the user conveying the new location.


You can send location data to the emulator using several other techniques, as shown in the DDMS user interface (see Figure 7-11). For example, the DDMS interface allows you to submit a GPS Exchange Format (GPX) file or a Keyhole Markup Language (KML) file. You can obtain sample GPX files from these sites:




Similarly, you can use the following KML resources to obtain or create KML files:



So you can upload a GPX or KML file to the emulator and set the speed at which the emulator will play back the file (see Figure 7-12). The emulator will then send location updates to your application based on the configured speed.

CI LogCat Q Emulator Control CI File Explorer CI Resource Explorer

Location Controls

Manual GPX KML

Manual GPX KML

Name Longitude Latitude

Eleva... Description

BOG Inn 175.628950 -38.583083

CAMP 175.613867 -38.639850

LINKRD 175.645467 -38.536233

MANGTU 175.608250 -38.679400

Pureora 175.628433 -38.551217

Rata 175.612038 -38.675838

Safe Park 175.674942 -38.709010

1165.0 PUREORA

683.9 14-MAR-09 12:20:47PM

543.0 13-MAR-09 8:37:30PM



Po... First Point Time

Last Point Time Comment

1165.0 PUREORA

683.9 14-MAR-09 12:20:47PM

543.0 13-MAR-09 8:37:30PM


Po... First Point Time

Last Point Time Comment

Day 2 Day 3

395 Mon Apr 13 16:33:3... Mon Apr 13 22:23:2... 472 Tue Apr 14 15:13:2... Tue Apr 14 23:43:3...

Day 2 Day 3

395 Mon Apr 13 16:33:3... Mon Apr 13 22:23:2... 472 Tue Apr 14 15:13:2... Tue Apr 14 23:43:3...

Figure 7-12. Uploading GPX and KML files to the emulator for playback

0 0

Post a comment