Search code examples
androidlocationlistenergsp

Getting an updated location throughout my app


I need to have an updated location throughout my app. I'm trying to do so by adding the code shown below in all my activities. My problem is that I can't be sure the user will stay in one activity long enough to get the update. How can I have some application-wise listener to achieve an updated position every given time (say 30min)?

private LocationManager mLocationManager;   

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    // My activity stuff
}       

private Location getBestLocation(LocationManager locationManager) {

    Location location_gps = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    Location location_network = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

    // If both are available, get the most recent
    if(location_gps!=null && location_network !=null) {
        return (location_gps.getTime() > location_network.getTime())?location_gps:location_network;
    }
    else if(location_gps==null && location_network ==null){
        return null;
    }
    else
        return (location_gps==null)?location_network:location_gps;

}

@Override
protected void onResume() {
    super.onResume();
    // Request location updates at startup
    mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, LOCATION_UPDATE_MIN_TIME, LOCATION_UPDATE_MIN_DIST, this);
    mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, LOCATION_UPDATE_MIN_TIME, LOCATION_UPDATE_MIN_DIST, this);
    getBestLocation(mLocationManager)
}

@Override
protected void onPause() {
    super.onPause();
    // Remove the locationlistener updates when Activity is paused
    mLocationManager.removeUpdates(this);
}

@Override
public void onLocationChanged(Location location) {
    GlobalVars.lat = (Double) (location.getLatitude());
    GlobalVars.lng = (Double) (location.getLongitude());  
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // Nothing here

}

@Override
public void onProviderEnabled(String provider) {
    Location location = getBestLocation(mLocationManager);
    // Initialize the location fields
    if (location != null) {             
        GlobalVars.lat = (Double) (location.getLatitude());
        GlobalVars.lng = (Double) (location.getLongitude());                    
    }
}

@Override
public void onProviderDisabled(String provider) {        
}

Solution

  • After further investigation, I ended up using a service.

    public class LocationService extends Service implements LocationListener {
    
        LocationManager locationManager; 
    
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    
        @Override
        public void onCreate() {
    
            locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
    
            Location loc = getBestLocation(locationManager);
            GlobalVars.lat = (Double) (loc.getLatitude());
            GlobalVars.lng = (Double) (loc.getLongitude());
    
        }
    
        public void onLocationChanged(Location loc) {        
            GlobalVars.lat = (Double) (loc.getLatitude());
            GlobalVars.lng = (Double) (loc.getLongitude());       
        }
    
        public static Location getBestLocation(LocationManager locationManager) {
    
            Location location_gps = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            Location location_network = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    
            // If both are available, get the most recent
            if(location_gps!=null && location_network !=null) {
                return (location_gps.getTime() > location_network.getTime())?location_gps:location_network;
            }
            else if(location_gps==null && location_network ==null){
                return null;
            }
            else
                return (location_gps==null)?location_network:location_gps;
    
        }
    
        public void onProviderEnabled(String s){}
        public void onProviderDisabled(String s){}
        public void onStatusChanged(String s, int i, Bundle b){}
    
        @Override
        public void onDestroy() {       
            locationManager.removeUpdates(this);
        }
    }