views:

35

answers:

1

I have an android application using LocationManager get the cell network location and not the Wifi location? If I have turned off the Wifi antenna and do the following:

LocationManager lm = (LocationManager) paramContext.getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

I am always returned the Wifi location and not the cell network location. I want it to return the cell location since I have moved fromthe Wifi location. I tried using a LocationListener but that doesn't seem to help.

This is running on an HTC Evo running Froyo.

Thanks!

A: 

Check out this section in the Android Dev Guide: Requesting Location Updates. Basically, you need to register a listener to get location updates, and then you tell Android that you want to get those updates with the network location provider.

// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
      // Called when a new location is found by the network location provider.
      makeUseOfNewLocation(location);
    }

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

    public void onProviderEnabled(String provider) {}

    public void onProviderDisabled(String provider) {}
  };

// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

You should then be able to get the last known location with getLastKnownLocation() and/or use the onLocationChanged() in the LocationListener.

Marc Bernstein
Marc - Network_Provider includes the Wifi provider AND Cell provider so I am always getting the Wifi last location that is old and the cell network has a more accurate answer. I'd like to know how to force it to forget the Wifi location and use the cell location.
Tony G.
Sorry Tony, I did not know that the wifi connection also provides location information. But I think this should still work. If you turn off the wifi and then use requestLocationUpdates() with the LocationListener, you should then start getting the cell network provided locations. If you only use getLastKnownLocation() without registering a LocationListener for updates then it will always return potentially outdated location info. I could be wrong about this but it's worth a try.
Marc Bernstein