Here's how to get started with a location service from GPS and network providers (Wi-Fi / data).
LocationManager locationManager = (LocationManager)getContext().getSystemService(Context.LOCATION_SERVICE); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener); locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,locationListener);
If you want to stop listening to location updates, run this code: locationManager.removeUpdates(locationListener);
One line of code stops listening to any location updates regardless of the provider (GPS / Network), because the LocationManager does not care about where the updates come from.
In your case, I suppose you know how to create some kind of user interface to let the user decide whether to use GPS / network or not. And then you can do something like this:
if ( useGPS ) { locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener); } if ( useNetwork ) { locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,locationListener); }
If the user has enabled both providers, location updates may be more accurate. And if the user has disabled both providers, it does not matter. Since there will be no updates in the LocationListener, this should be what the user wanted.
By the way, here is the code to create a LocationListener:
LocationListener locationListener = new LocationListener() { @Override public void onLocationChanged(Location location) {