I have a service that gets my location and it is updated every few seconds in "onLocationChanged" (that works well), that's when I have to call my AsyncTask repeatedly to send the coordinates and they arrive at my web service , but the problem is that asycntask
never runs ... I have read that my solution is in the use of " delegates " but I can not understand how they work.
My service
public class ServicePosition extends Service {
private static final String TAG = "TESTGPS";
private LocationManager mLocationManager = null;
private static final int LOCATION_INTERVAL = 1000;
private static final float LOCATION_DISTANCE = 10f;
UserSessionManager session;
private class LocationListener implements android.location.LocationListener{
Location mLastLocation;
public LocationListener(String provider)
{
Log.e(TAG, "LocationListener " + provider);
mLastLocation = new Location(provider);
}
@Override
public void onLocationChanged(Location location)
{
//Se actualiza cada vez que cambio de posición
double latitud = location.getLatitude();
double longitud = location.getLongitude();
Log.e(TAG, "onLocationChanged: " + location);
mLastLocation.set(location);
JSONObject posicionActual = new JSONObject();
try {
posicionActual.put("Latitud", latitud);
posicionActual.put("Longitud", longitud);
new AsyncTaskServerPosition.SendToServer().execute(posicionActual.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onProviderDisabled(String provider)
{
Log.e(TAG, "onProviderDisabled: " + provider);
}
@Override
public void onProviderEnabled(String provider)
{
Log.e(TAG, "onProviderEnabled: " + provider);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
Log.e(TAG, "onStatusChanged: " + provider);
}
}
LocationListener[] mLocationListeners = new LocationListener[] {
new LocationListener(LocationManager.GPS_PROVIDER),
new LocationListener(LocationManager.NETWORK_PROVIDER)
};
@Override
public IBinder onBind(Intent arg0)
{
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.e(TAG, "onStartCommand");
super.onStartCommand(intent, flags, startId);
return START_NOT_STICKY;
}
@Override
public void onCreate()
{
Log.e(TAG, "onCreate");
initializeLocationManager();
try {
mLocationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
mLocationListeners[1]);
} catch (java.lang.SecurityException ex) {
Log.i(TAG, "No puede solicitar la actualización de la ubicacion", ex);
} catch (IllegalArgumentException ex) {
Log.d(TAG, "El proveedor de gps no existe, " + ex.getMessage());
}
try {
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
mLocationListeners[0]);
} catch (java.lang.SecurityException ex) {
Log.i(TAG, "No puede solicitar la actualización de la ubicacion", ex);
} catch (IllegalArgumentException ex) {
Log.d(TAG, "El proveedor de gps no existe " + ex.getMessage());
}
}
@Override
public void onDestroy()
{
Log.e(TAG, "onDestroy");
super.onDestroy();
if (mLocationManager != null) {
for (int i = 0; i < mLocationListeners.length; i++) {
try {
mLocationManager.removeUpdates(mLocationListeners[i]);
} catch (Exception ex) {
Log.i(TAG, "fail to remove location listners, ignore", ex);
}
}
}
}
private void initializeLocationManager() {
Log.e(TAG, "initializeLocationManager");
if (mLocationManager == null) {
mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
}
}}
My AsyncTask
public class AsyncTaskServerPosition {
private static final String TAG = "ASYNCTASK";
private static UserSessionManager session;
public static class SendToServer extends AsyncTask<String, String, String>
{
@Override
protected void onPreExecute() {
}
@Override
protected String doInBackground(String... coordenadas) {
try {
HashMap<String, String> user = session.obtenerRolyId();
String usuarioId = user.get(UserSessionManager.KEY_ID);
Log.d(TAG, "Location: " +coordenadas);
HttpURLConnection urlConnection = null;
String posicionActual = coordenadas[0];
BufferedReader reader = null;
OutputStream os = null;
InputStream inputStream = null;
URL url = new URL("http://localhost:8081/odata/Usuarios("+usuarioId+")/ActualizarPosicion");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestProperty("Accept", "application/json");
urlConnection.connect();
os = new BufferedOutputStream(urlConnection.getOutputStream());
os.write(posicionActual.getBytes());
os.flush();
os.close();
int serverResponse = urlConnection.getResponseCode();
String serverMsg = urlConnection.getResponseMessage();
urlConnection.disconnect();
Log.d(TAG, "Code: " + serverResponse + " - Menssage: " + serverMsg);
} catch (Exception e) {
Log.i("error", e.toString());
}
return "call";
}
@Override
protected void onPostExecute(String result) {
}
}}