Localização GPS Android

Escrevi esta classe LocationHelper para obter a localização GPS (ou wi-fi):

public class LocationHelper {

private MyLocation location;

Context context;

Timer timer;
LocationManager lm;
boolean gps_enabled=false;
boolean network_enabled=false;
ILocation activity;

private LocationHelper(){
}

public LocationHelper(Context context){
this();

this.context = context;
location
= new MyLocation();
}

public LocationHelper(Context context, ILocation activity){
this(context);

this.activity = activity;
}

LocationListener locationListenerGps = new LocationListener() {
public void onLocationChanged(Location location) {
timer
.cancel();
gotLocation
(location);
// gps will keep going
lm
.removeUpdates(locationListenerNetwork);
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};

LocationListener locationListenerNetwork = new LocationListener() {
public void onLocationChanged(Location location) {
timer
.cancel();
gotLocation
(location);
lm
.removeUpdates(this);
lm
.removeUpdates(locationListenerGps);
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};

class GetLastLocation extends TimerTask {
@Override
public void run() {
lm
.removeUpdates(locationListenerGps);
lm
.removeUpdates(locationListenerNetwork);

Location net_loc=null, gps_loc=null;
if(gps_enabled){
gps_loc
=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
if(network_enabled){
net_loc
=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}

//if there are both values use the latest one
if(gps_loc!=null && net_loc!=null){
if(gps_loc.getTime() > net_loc.getTime()){
gotLocation
(gps_loc);
}else{
gotLocation
(net_loc);
}
return;
}

if(gps_loc!=null){
gotLocation
(gps_loc);
return;
}
if(net_loc!=null){
gotLocation
(net_loc);
return;
}

gotLocation
(null);
}
}

private void gotLocation(Location newLocation){
if(newLocation != null){
if(activity != null){
activity
.updateDirectionOnObjects(newLocation);
}

location
.setLatitude(newLocation.getLatitude());
location
.setLongitude(newLocation.getLongitude());
}
}

public MyLocation getLocation() {
return location;
}

public void register() {
if(lm==null){
lm
= (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
}

//exceptions will be thrown if provider is not permitted.
try{gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){}
try{network_enabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}catch(Exception ex){}

//don't start listeners if no provider is enabled
if(gps_enabled || network_enabled){
if(gps_enabled){
lm
.requestLocationUpdates(LocationManager.GPS_PROVIDER, 12000, 0, locationListenerGps);
}
if(network_enabled){
lm
.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
}

timer
=new Timer();
timer
.schedule(new GetLastLocation(), 20000);
}
}

public void unregister() {
lm
.removeUpdates(locationListenerGps);
lm
.removeUpdates(locationListenerNetwork);
}
}

MyLocation é uma classe de modelo simples:

public class MyLocation {
private double latitude;
private double longitude;

public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
}

ILocation é uma interface que pode ser implementada se você quiser um feedback quando o local for encontrado:

public interface ILocation {
public void updateDirectionOnObjects(Location location);
}

Para usá-lo, você terá que criar um objeto LocationHelper:

private LocationHelper locationHelper;

No método onCreate:

locationHelper = new LocationHelper(this);
//If you want to use the feedback-method:
//locationHelper = new LocationHelper(this,this);
//Note: you will have to implement ILocation for this to work!

No onResume-Method (não onCreate!), Registre o ouvinte:

locationHelper.register();

No método onPause, cancele o registro do ouvinte:

locationHelper.unregister();

Se você não implementou ILocation, pode obter o local atual com a seguinte chamada:

locationHelper.getLocation();