Getting the location in Android programatically
        Posted  
        
            by 
                steveo225
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by steveo225
        
        
        
        Published on 2010-12-31T06:36:50Z
        Indexed on 
            2011/01/09
            1:54 UTC
        
        
        Read the original article
        Hit count: 304
        
I know this has been asked a ton, so my apologies. I have the following code, and cannot get the location, always a null response. I am trying to avoid a LocationListener in this instance because I am already using an update Service, and the location really doesn't have to be that fine, so the last known location is good enough. Thanks for the help.
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
String providers[] = {"gps", "network", "passive"};
Location loc = null;
for(String x : providers) {  
    loc = lm.getLastKnownLocation(x);
    if(loc != null) break;  
}
if(loc != null) {
    // do something, never reached
}
© Stack Overflow or respective owner