How to get Android LocationManager to use a cellular network rather than Wi-Fi?

Do I have an Android app using LocationManager to get cell location, not Wifi location? If I unplug the Wifi antenna and do the following:

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

I always return the Wifi location, not the cell location. I want it to return the location of the cell since I moved from the Wifi location. I tried using LocationListener but it doesn't seem to help.

It works on the HTC Evo that Froyo runs on.

Thank!

+3
source share
3 answers

I do not believe that you can.

wifi , , , Google (GLS) MASF. , . NetworkProvider , . : NetworkLocationProvider.

, . PhoneStateListener.

+1

Android Dev: . , , , Android, .

// 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);

getLastKnownLocation() / onLocationChanged() LocationListener.

0

, , , ? .

 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;
}
0

Source: https://habr.com/ru/post/1770338/


All Articles