Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switching between gps and network provider according to the availability

public void onCreate() {
 locationListener = new GeoUpdateHandler();
 locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

try {
   gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
   network_enabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex) { 
}

if(gps_enabled) {
       locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, TIME_DURATION, 100, locationListener);
} else if(network_enabled) { // Checking for GSM
   locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, TIME_DURATION, DISTANCE, locationListener);
}   
}   
public class GeoUpdateHandler implements LocationListener {
private void getPresentLocation(Location location) {
    ... 
            //Fetch the locations
            ...
}

@Override
public void onProviderDisabled(String provider) { }

@Override
public void onProviderEnabled(String provider) { }

@Override
public void onStatusChanged(String provider, int status, Bundle extras) { }

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

this is my code snippet and my issue is that once the application is installed with GPS Switched ON, then it is not switching to GSM to fetch location once we OFF GPS, and vice versa.

So please tell me a way to switch efficiently between GPS and GSM according to the availability of the location provider.

NOTE: My app is a service.

Thanks.

Edit: I got a BroadcastReceiver, thats why i assume the onCreate() will be invoked once the GPS ON/OFF happen.

I also tried checking the provider availability in onLocationChanged(Location location). But it didnt helped.

EDIT: I modified my onProviderDisabled & onProviderEnabled like this, Please tell what i am doing wrong here..

    public void onProviderDisabled(String provider) {
        locationManager.removeUpdates(locationListener);
        if (provider.toLowerCase().equals(LocationManager.GPS_PROVIDER)) {
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, TIME_DURATION, 1, locationListener);               
        } else {
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, TIME_DURATION, 1, locationListener);
        }   
    }

    @Override
    public void onProviderEnabled(String provider) {
        locationManager.removeUpdates(locationListener);
        if (provider.toLowerCase().equals(LocationManager.GPS_PROVIDER)) {
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, TIME_DURATION, 1, locationListener);
        } else {
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, TIME_DURATION, 1, locationListener);
        }

    }
like image 941
Lenin Avatar asked Dec 28 '11 12:12

Lenin


1 Answers

Got a solution for this:

 public void onProviderDisabled(String provider) {
   locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
   if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,  locationListener);
   } else {
      locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
   } 
}

@Override
public void onProviderEnabled(String provider) {
  locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);   
  if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
  } else {
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
  }
}

what i missed is locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); in onProviderEnabled() and onProviderDisabled().

Thanks

like image 173
Lenin Avatar answered Sep 28 '22 05:09

Lenin