In the end the best solution I found was to use Service, as follows:
public class GpsService extends Service implements LocationListener{
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
LocationManager LocManager =(LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
LocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 0, this);
return Service.START_STICKY;
}
public void onLocationChanged(Location location) {
Log.i("location", String.valueOf(location.getLongitude()));
Log.i("location", String.valueOf(location.getLatitude()));
}
public void onProviderDisabled(String provider) {
Log.i("info", "Provider OFF");
}
public void onProviderEnabled(String provider) {
Log.i("info", "Provider ON");
}
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.i("LocAndroid", "Provider Status: " + status);
Log.i("info", "Provider Status: " + status);
}
}
And in onCreate () I launch the service:
public class PacienteApp extends Application {
@Override
public void onCreate() {
Intent intent = new Intent(this, GpsService.class);
this.startService(intent);
}
}
Using Broadcast Receiver is a good idea, but laborious as they are not allowed to use handlers within this class. And anyway you need the service to run in the background.