Connecting to a Location Provider and Getting Location Updates

Let's start with an Activity that just connects with the GPS LocationProvider and gets and displays our current location (no updates). The procedure is pretty straightforward: package com.microjobsinc.dloc;

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

public class Main extends Activity {

/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

// find the TextViews

TextView tvLatitude = (TextView)findViewById(R.id.tvLatitude);

TextView tvLongitude = (TextView)findViewById(R.id.tvLongitude);

// get handle for LocationManager

LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);©

// connect to the GPS location service

Location loc = lm.getLastKnownLocation("gps");

// fill in the TextViews tvLatitude.setText(Double.toString(loc.getLatitude()));

tvLongitude.setText(Double.toString(loc.getLongitude()));

Here are some of the highlights of the code:

O Connect to the LocationManager using getSystemService(Context.LOCATION_SERV ICE).

© Ask the LocationManager where we are using getLastKnownLocation("provider").

© Get the latitude and longitude from the location returned and use it as needed.

But we also want to get periodic location updates from the LocationManager so we can track our location as we move about. For that we need to add a listener routine and ask the LocationManager to call it when it has an update.

Location updates from the LocationManager are accessible to an application through a DispLocListener class, so we will create an instance of this class in the onCreate method of our main Activity. We are required to override a number of methods in DispLocListener to meet the LocationListener interface definition, but we don't need them for this application, so we'll leave the definitions empty.

The full implementation follows: package com.microjobsinc.dloc;

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.TextView;

public class Main extends Activity { private LocationManager lm; private LocationListener locListenD; public TextView tvLatitude; public TextView tvLongitude;

/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

// find the TextViews tvLatitude = (TextView)findViewById(R.id.tvLatitude); tvLongitude = (TextView)findViewById(R.id.tvLongitude);

// get handle for LocationManager LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

// connect to the GPS location service

Location loc = lm.getLastKnownLocation("gps"); // fill in the TextViews tvLatitude.setText(Double.toString(loc.getLatitude())); tvLongitude.setText(Double.toString(loc.getLongitude()));

// ask the Location Manager to send us location updates locListenD = new DispLocListener();

lm.requestLocationUpdates("gps", 30000L, 10.0f, locListenD);

private class DispLocListener implements LocationListener { @Override public void onLocationChanged(Location location) { // update TextViews tvLatitude.setText(Double.toString(location.getLatitude())); tvLongitude.setText(Double.toString(location.getLongitude()));

@Override public void onProviderDisabled(String provider) { }

@Override public void onProviderEnabled(String provider) { }

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

Our onCreate method creates an instance of DispLocListener and requests that the LocationManager update it as needed using requestLocationUpdates. This method takes four parameters:

String provider

Which location provider to use. We assume GPS is available in this case. long minTime

Minimum update time, in milliseconds. The LocationManager will wait at least this long between updates. Here's an opportunity to tune your application for battery life: more frequent updates means more battery usage.

float minDistance

Minimum distance, in meters, required to trigger an update. The Location Manager will update us only if we've moved at least this far since the last update. LocationListener listener

The name of the listener method to call when there is an update. This is the DispLocListener instance we just created.

Finally, we want to add the onPause and onResume code to turn off location updates when we're not actually displaying on the user's screen, and then turn them back on when we are: /**

* Turn off location updates if we're paused */

^Override public void onPause() { super.onPause(); lm.removeUpdates(locListenD);

* Resume location updates when we're resumed */

^Override public void onResume() { super.onResume();

lm.requestLocationUpdates("gps", 30000L, 10.0f, locListenD);

0 0

Post a comment