Mathias
Mathias

Reputation: 3981

Android's network_provider locationprovider never returns anything if WiFi is off?

Would be great if someone could assist. I am trying to setup positioning in my Android app and try and register locationlisteners on both the GPS and NETWORK providers, as per all examples out there.

However, my network provider never responds anything network related, i.e. only if WiFi is enabled do i get a response. If not, it always times out.

My code:

lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 1, locationListener);
....
public void onLocationChanged(Location location) {
        Log.d(TAG, "onLocationChanged;provider;" + location.getProvider() + ",lon;" + location.getLongitude() + ";lat;" + location.getLatitude() + ";accuracy;" + location.getAccuracy());
        resultHandler.handleLocationResult(location, false);
        lm.removeUpdates(this);

So, if Wifi is on i get a response pretty quick, but if i only have GSM on, nothing happens, and i see nothing in the android logcat either.

I have tried changing the params in the requestlocationupdates aswell, no difference.

If there is anything i can do different, pointers would be MUCH appreciated!

EDIT: of course, i've tried the network positioing other ways, via carrier positioning (using sms) which works, tried same setup with google maps, maps can get position i think. Of course i have reception :)

Upvotes: 2

Views: 485

Answers (1)

Johnny Sørensen
Johnny Sørensen

Reputation: 209

I know this is almost a year old, but for others searching for the same issue, I believe this is due to onLocationChanged only being called whenever the actual NETWORK_PROVIDER returns a different location.

So in this case, you should use the cached NETWORK_PROVIDER location if you reach some sort of timeout, because the cached location should be the current.

i.e.:

Location lastNetworkLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

Then check if the getTime() and distanceTo() are reasonable.

Upvotes: 4

Related Questions