Question

I'm using LocationManager's method requestLocationUpdates to get the location of the cell phone.

I'm using GPS as the Provider and working with a LocationListener to update the UI of my app.

I need to call a method on my Activity every 1000 milliseconds. To do that, I configured the requestLocationUpdates like this:

locationManager.requestLocationUpdates(provider, 0, 1000, listener);

Everything is working well. The onLocationChanged method is called and the Latitude and Logitude are showed ok.

But I have some doubts.

  1. What happens if I'm in a place with no GPS signal?.
  2. There is some automatic Timeout?
  3. Or the App will try to get the position using the GPS all the time and will drain the battery.
  4. What happens if I switch to WiFI/3G Provider and thats signals also aren't working? It happens the same?

I search over Internet looking for the answer but I can't find the right solution.

Was it helpful?

Solution

For example let's say you are driving in a car and the LocationManager is connected to a valid GPS provider. Now you driving through a tunnel and lose the signal. Now onLocationChanged() isn't triggered anymore. But the system will keep searching in background for a valid GPS provider. If you are through the tunnel the system reconnects to the GPS provider, if still available, and onLocationChanged() is triggered again.

The drain of the battery while connected and getting location updates is much higher as when the system is looking for a valid provider.

If you switch to WiFi or 3G the system will look for the last set provider and overrides the previous one. But the behaviour will be the same.

You can react on this event by monitoring the LocationListener.onStatusChanged() callback and the GpsStatus.Listener interface. E.g. switch to another provider or if no provider is available call removeLocationUpdates() and call requestLocationUpdates() again after a period of time with a Timer. This will save your battery much better.

Edit:

LocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) returns true if GPS is enabled in the settings of the device. It's no indicator that you are connected to a GPS provider. Use the method LocationManager.getBestProvider(Criteria, boolean) to know which provider is available.

As a note: You will almost never get a connection to GPS if you are inside of a room.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top