title: Is it correct to call an asynctask inside a service?
body: <p>I try to call an asynctask in the "onLocationChanged" method but the asynctask never receives the parameters. My service gets my coordinates for every change in my location and works well showing them in console.</p>
  
  <p><strong>In my Service</strong></p>
  
  <pre><code>public void onLocationChanged(Location location){
  
          double latitude = location.getLatitude();
          double longitude = location.getLongitude();
  
          Log.e(TAG, "onLocationChanged: " + location);
          mLastLocation.set(location);
  
          JSONObject actualPosition = new JSONObject();
  
          try {
  
              actualPosition.put("Latitude", latitude);
              actualPosition.put("Longitude", longitude);
  
              new AsyncTaskServerPosition.SendToServer().execute(actualPosition.toString());
  
          } catch (JSONException e) {
              e.printStackTrace();
          }
  
      }
  </code></pre>
  
  <hr>
  
  <p><strong>Edit</strong></p>
  
  <p><strong>My asynctask</strong></p>
  
  <pre><code>public class AsyncTaskServerPosition{
  
  private static final String TAG = "ASYNCTASK";
  private static UserSessionManager session;
  
  
  public static class SendToServer extends AsyncTask&lt;String, String, String&gt;
  {
  
      @Override
      protected void onPreExecute() {
  
      }
  
      @Override
      protected String doInBackground(String... location) {
  
          try {
  
              HashMap&lt;String, String&gt; user = session.obtenerRolyId();
              String userId = user.get(UserSessionManager.KEY_ID);
  
              Log.d(TAG, "Location: " +location);
              HttpURLConnection urlConnection = null;
              String actualPosition = location[0];
              BufferedReader reader = null;
              OutputStream os = null;
              InputStream inputStream = null;
  
              URL url = new URL("http://localhost:8081/odata/Usuarios("+userId+")/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(actualPosition.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) {
      }
  
  }}
  </code></pre>
  
  <p><strong>Service complete</strong></p>
  
  <pre><code>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)
      {
  
          double latitude = location.getLatitude();
          double longitude = location.getLongitude();
  
          Log.e(TAG, "onLocationChanged: " + location);
          mLastLocation.set(location);
  
          JSONObject actualPosition = new JSONObject();
  
          try {
  
              actualPosition.put("Latitud", latitude);
              actualPosition.put("Longitud", longitude);
  
              new AsyncTaskServerPosition.SendToServer().execute(actualPosition.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 &lt; 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);
      }
  }}
  </code></pre>