1. Stop listening to network location updates: This is very simple. Just call
your_loc_manager.removeUpdates(network_location_listener);
2. Obtaining a location from the network provider if the GPS provider does not provide any corrections. You can try something like the following (instead of a timer, you can try using the GpsStatus Listener ...)
inside the method of receiving network provider updates
boolean network_enabled ; try { network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); } catch(Exception ex) { ex.printStackTrace(); } if(!network_enabled) return; locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 100, networkLocationListener); return;
Inside GPS Location Method
boolean gps_enabled; try { gps_enabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER); } catch(Exception ex) { ex.printStackTrace(); } if(!gps_enabled) { // call your method for getting network location updates return; } locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 50, locationListenerGps); new Timer().schedule(new TimerTask() { @Override public void run() { // You should call your method of getting Network locations based on some // condition which would tell whether you really need to ask for network location } }, 20000 /* Time Value after which you want to stop trying for GPS and switch to Network*/);
EDIT 1:
Also "I donโt need to know when to receive updates from GPS, I need to know when to receive updates from the network (I mean, I need to know when gps is looking, and not when it is fixed) and how to control interchangeability between the network and GPS "
... If the fix was lost, you only need to look for network updates, so I suggested calling
if (hasGPSFix) { Log.i("GPS","Fix Lost (expired)"); lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener); lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 2000, 10, locationListener); //===> new line which I am suggesting } hasGPSFix = false;
Thus, you will receive a location update from the network, while GPS will continue to attempt corrections. After reaching the GPS installation, hasGPSFix will change its value and the existing code in the specified link will stop searching for network updates. At least this is something that I could understand what you want to achieve. Perhaps I didnโt completely understand you.
EDIT 2: Here is my class for working with GPS and NETWORK providers.
In MapActivity, or where you need to get location updates, you need to instantiate this MyLocation class. And also, in order to receive location updates, you need to implement LocationResult in your call activity. public abstract void gotLocation(Location location, boolean isGpsData); will notify you if the update you received now appears from the network or GPS. In addition, in case of returning data from GPS, it automatically closes the netowrk receiver. You will need to call the getFineLocation() method if you need data from GPS, etc. You will need to improve this code for your purpose (for example, you need to improve the closeListeners() method to close only one provider, when you just want to remove only network updates, etc.), but it gives the basic implementation that you you want (along with the implementation of GpsStatus.Listener , of course, so both combined should serve your purpose well).
public class MyLocation { LocationManager locManager; LocationResult locationResult; boolean gps_enabled = false; boolean network_enabled = false; public boolean getCoarseLocation(Context context, LocationResult result) { locationResult = result; if(locManager == null) locManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); try { network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); } catch(Exception ex) { ex.printStackTrace(); } if(!network_enabled) return false; locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 100, networkLocationListener); return true; } public boolean getFineLocation(Context context, LocationResult result) { locationResult = result; if(locManager == null) locManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); try { gps_enabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER); } catch(Exception ex) { } if(!gps_enabled) return false;//No way to get location data locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 100, locationListenerGps); return true; } LocationListener locationListenerGps = new LocationListener() { public void onLocationChanged(Location location) { locationResult.gotLocation(location,true); locManager.removeUpdates(networkLocationListener); } public void onProviderDisabled(String provider) {} public void onProviderEnabled(String provider) {} public void onStatusChanged(String provider, int status, Bundle extras) {} }; LocationListener networkLocationListener = new LocationListener() { public void onLocationChanged(Location location) { locationResult.gotLocation(location,false); } public void onProviderDisabled(String provider) {} public void onProviderEnabled(String provider) {} public void onStatusChanged(String provider, int status, Bundle extras) {} }; public void closeListeners() { if(locManager == null) return; if(locationListenerGps != null) locManager.removeUpdates(locationListenerGps); if(networkLocationListener != null) locManager.removeUpdates(networkLocationListener); } public static abstract class LocationResult { public abstract void gotLocation(Location location, boolean isGpsData); } public static boolean hasNetworkProvider(Context mContext) { List<String> myProvidersList = ((LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE)).getProviders(false); return myProvidersList.contains("network"); } public static boolean hasGPSProvider(Context mContext) { List<String> myProvidersList = ((LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE)).getProviders(false); return myProvidersList.contains("gps"); } }