Switching between network and GPS provider
Asked Answered
E

2

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?


Expectant answered 31/3, 2011 at 19:45 Comment(0)
R
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.

Rile answered 31/3, 2011 at 19:56 Comment(7)
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!Expectant
There's a LocationManager.addGpsStatusListener() method. When it tells you something changed, act accordingly.Rile
Can't get this to work for some reason! Although I added the gpsStatusListener to the locationManager the onGpsStatusChanged() is never called!Expectant
You can request location updates for both providers. The API supports more than one provider. Their sample location app shows the usage: <code.google.com/p/apps-for-android/source/browse/trunk/Radar/…>Rile
Oh yeah I know I can do that but I want to use either the one or the other!Expectant
@Lenin Sorry for the late reply! It kinda work at the time but not very well...Cant remember what the exact problem was tho!Expectant
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.Pettigrew
C
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);
    }
}
Carmel answered 8/4, 2011 at 14:35 Comment(4)
This is not flexible enough.. it's better to get both and decide which is betterView
@View How can you decide that?Zaffer
@Hector Read this: developer.android.com/guide/topics/location/strategies.htmlView
Whichever strategy you adopt, you'll need to inform your users if your selected GPS location method will consume a little or a lot of battery power on their devices.Molecule

© 2022 - 2024 — McMap. All rights reserved.