1

I think I understood that network location polling requires an internet connection in order to retrieve the correct location (lat/lon) associated to the cell towers and wifi networks around. Correct?

Then this is my problem: I have an app that polls location, and it works fine when wifi/3g is on. However, when wifi/3g is off, it keeps on giving the last known location (instead of nothing), although I don't ask for the last location fix it in my code. Is there some kind of caching involved and if yes how to ignore it?

Thanks

znat
  • 13,144
  • 17
  • 71
  • 106
  • I dont think there is internet required for getting location from Network and GPS provider.It will be required in case of location from Wifi – Alok Kulkarni Jan 04 '12 at 18:16
  • Maybe [this](http://stackoverflow.com/a/3145655/340068) will help you – Alok Kulkarni Jan 04 '12 at 18:18
  • Thank you Alok. This is the code I am actually using to get my locations (I removed the last known part) but when offline (no internet connection) it keeps on returning the last location – znat Jan 04 '12 at 18:54
  • Are you not able to get location using network and/or GPS ? – Alok Kulkarni Jan 04 '12 at 19:08
  • GPS works fine, my problem only concerns network location when the device is offline. I keep on having the last location returned and I don't understand why since I don't ask for the last known location – znat Jan 04 '12 at 19:37
  • It seems that getting correct location is a tough job in Android.Need to investigate more – Alok Kulkarni Jan 04 '12 at 19:45

1 Answers1

0

I solved this issue by adding a check for any connected network before requesting location updates of NETWORK_PROVIDER.

public boolean isNetworkAvailable() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected()) {
            return true;
        }
        return false;
}

It's solution for a simple request. If you want to determine when the network is on/off and request/remove location updates, then you must use features of ConnectivityManager. It sends broadcast intents when network connectivity changes.

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
gio
  • 4,950
  • 3
  • 32
  • 46