Pergunta

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!

Foi útil?

Solução

I don't believe you can.

Network location provider look at the wifi state and cell state, discard one if too old, or send them both over to the Google Location Server (GLS) via MASF server. What's done there is magic. But you can look at the implementation of NetworkProvider and see how to tweak your code. Codes are here: NetworkLocationProvider.

If you just want the cell ID, check out Telephony Manager. Add a PhoneStateListener there.

Outras dicas

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.

I don't if you have an answer by now, but did you try to write some logic on network info and get type of network you are in ?? and do a location service call.

 public boolean haveNetworkConnection()
{
    boolean haveConnectedWifi = false;
    boolean haveConnectedMobile = false;

    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo[] netInfo = cm.getAllNetworkInfo();
    for (NetworkInfo ni : netInfo)
    {
        if (ni.getTypeName().equalsIgnoreCase("WIFI"))
            if (ni.isConnected())
                haveConnectedWifi = true;
        if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
            if (ni.isConnected())
                haveConnectedMobile = true;
    }
    return haveConnectedWifi || haveConnectedMobile;
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top