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?