Daily Archives: 2011-03-18


Using two location providers on Android


Android has two kinds of accuracy on location:

  • Fine: provided by the GPS, needs some time to be obtained
  • Coarse: location determined with the cell of the mobile network

This location methods can be enabled or disabled by the user on the preferences or with some widgets.

Initially on our apps we used only one LocationProvider with “Fine” accuracy:

  • If the GPS was disabled it used automatically network-based location
  • But if GPS was enabled, the location used it needing some time to be determined

As the data couldn’t be obtained until the location is determined, the app didn’t showed data, receiving this kind of error reports from some users.

The best solution that I found is to mantain two separated providers, with different precisions and receive location updates using both.

LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(false);
criteria.setPowerRequirement(Criteria.POWER_LOW);       

criteria.setAccuracy(Criteria.ACCURACY_FINE);
String providerFine = manager.getBestProvider(criteria, true);

criteria.setAccuracy(Criteria.ACCURACY_COARSE);
String providerCoarse = manager.getBestProvider(criteria, true);

if (providerCoarse != null) {
    manager.requestLocationUpdates(providerCoarse, 5*60000, 100, this);
}
if (providerFine != null) {
    manager.requestLocationUpdates(providerFine, 5*60000, 100, this);
}

You can also check if providerFine and providerCoarse are the same provider. When receiving location, the one received from providerFine must take precedence over the one from providerCoarse. Location’s provider can be obtained with location.getProvider():

public void onLocationChanged(Location location) {
     if (location.getProvider().equals(providerFine)) {
     ...

This is a trick that we are using on our Gas Stations Spain app and also on Wikiplaces (open sourced).