Sandra
Sandra

Reputation: 4259

Should I use separate locationManager for GPS and Network Provider?

Can I use the same locationManager for requesting updates for the gps and network provider, or should I create two locationManagers and separate onLocation changed functions etc. I got really confused about this

Upvotes: 1

Views: 1040

Answers (1)

citizen conn
citizen conn

Reputation: 15390

You will want to check and see which one is available in the order you want to use them and then use the corresponding one:

if(mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
    mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, LOCATION_UPDATE_MIN_TIME, LOCATION_UPDATE_MIN_DISTANCE, locationListener);
}
else if(mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
    mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, LOCATION_UPDATE_MIN_TIME, LOCATION_UPDATE_MIN_DISTANCE, locationListener);
}

You can use the same listener because all the listener's methods take as a parameter a Location object which is source agnostic:

@Override
public void onLocationChanged(Location location) {
    // updateLocation();
}

Upvotes: 1

Related Questions