10

I want to implement a locationListener which will switch between network and GPS providers based on availability.

For example if GPS is not enabled I want it to use network but as soon as GPS is on then I want it to stop listening for location updates from network and start listening from GPS.

Similarly I want it to start listening for updates from network as soon as GPS is switched off.

Is that possible?


Subquestion

Is GPS as fast as network in providing a location fix?


RedBlueThing
  • 42,006
  • 17
  • 96
  • 122
mixkat
  • 3,883
  • 10
  • 40
  • 58

2 Answers2

9

Sure, you just get the providers for the network and GPS and pass whichever you want to locationManager.requestLocationUpdates().

When you want to stop listening to a certain provider, call locationManager.removeUpdates() with the listener object you specified in locationManager.requestLocationUpdates().

Network:

Criteria crit = new Criteria();
crit.setPowerRequirement(Criteria.POWER_LOW);
crit.setAccuracy(Criteria.ACCURACY_COARSE);
String provider = locationManager.getBestProvider(crit, false);

GPS:

Criteria crit2 = new Criteria();
crit2.setAccuracy(Criteria.ACCURACY_FINE);
provider2 = locationManager.getBestProvider(crit2, false);

You can use LocationManager.isProviderEnabled() doc to see if the appropriate provider is enabled/disabled. There's more info available in the LocationManager docs.

GPS is usually much slower than network since you have to find 3+ far-away satellites, etc.

typo.pl
  • 8,812
  • 2
  • 28
  • 29
  • 1
    Yeah but when I initialise the locationManager to request location updates it will only listen from the provider that I'm passing in. i.e. `locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, minTime, minDistance, listener)` will only listen for updates from network and the onProviderEnabled will never get called for GPS! – mixkat Mar 31 '11 at 20:07
  • There's a `LocationManager.addGpsStatusListener()` method. When it tells you something changed, act accordingly. – typo.pl Mar 31 '11 at 20:18
  • Can't get this to work for some reason! Although I added the gpsStatusListener to the locationManager the `onGpsStatusChanged()` is never called! – mixkat Mar 31 '11 at 20:59
  • You can request location updates for both providers. The API supports more than one provider. Their sample location app shows the usage: – typo.pl Mar 31 '11 at 21:06
  • Oh yeah I know I can do that but I want to use either the one or the other! – mixkat Mar 31 '11 at 21:13
  • @Lenin Sorry for the late reply! It kinda work at the time but not very well...Cant remember what the exact problem was tho! – mixkat Jan 20 '12 at 14:55
  • I am having the very same problem, still dunno what should I do to accomplish that. In a case in which the application starts and getBestProvider returns null, I am using TimerTask to keep polling to check if the user has turned on any provider. If then any provider gets on, the listener sticks with it and I cannot change for a better provider, e.g., network provider => gps. If the first provider happens to be network, the listener sticks with it and does not matter if the user turn on gps later. – Eduardo Mar 29 '12 at 21:56
1

I am using this

LocationManager locationManager;
LocationListener locationListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    locationManager = (LocationManager) this
            .getSystemService(Context.LOCATION_SERVICE);
    String locationProvider = LocationManager.NETWORK_PROVIDER;
    Location lastKnownLocation = locationManager
            .getLastKnownLocation(locationProvider);
    if (lastKnownLocation == null) {
        locationProvider = LocationManager.GPS_PROVIDER;
        lastKnownLocation = locationManager
                .getLastKnownLocation(locationProvider);
    }
    if (lastKnownLocation != null) {
        makeUseOfNewLocation(lastKnownLocation);
    }
    locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            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
    if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    } else {
        locationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
    }
}
maza23
  • 555
  • 1
  • 5
  • 16