Geolocation - Android changing the location provider dynamically

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

In my app, I need to get the location of my users. Since my app is going to do it constantly, I want to change the location provider in certain conditions (between GPS and NETWORK providers). My app is going to detect such conditions and change it.

But, I m not sure how to change it dynamically.

So far, I have a thread (that, among other things, will check for these conditions) and I want it to change the location provider too. My code until now is as below:

public void run() {

    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    LocationListener locationController = new LocationController();

    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, time, desltaDistance, locationController );

    while (serviceIsRunning) {
        ...
        if (conditionsAreMet) {
            locationManager.removeUpdates(locationController);
            if (provider == LocationManager.GPS_PROVIDER)
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, time, desltaDistance, locationController );
            else
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, time, deltaDistance, locationController );
        }
        ...
    }
}
  • The LocationController class implements LocationListener.

But, it don t seems to be the better way to do it. So, what I want to know is: there is a better way to change the location provider automatically?

Answers

Since no one answered this question, here is how I figured it out.

Instead of make the change between the providers, I registered a constant controller for the NETWORK_PROVIDER. And, according to my conditions, a controller for the GPS_PROVIDER may be registered or not.

It s easier to register/unregister the locations request for one provider, instead of keep changing between them.

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/24394789/android-changing-the-location-provider-dynamically

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils