title: Android Query - detecting location
body: <p>I am using Android Query to detect user's location.</p>
  
  <p>For some reason, when I try to retrieve my latitude and longitude, it shows my previous location which is a couple hundred meters away, as if it was cached.</p>
  
  <p>What could be the issue here?</p>
  
  <pre><code>public class LocationHelper {
      private Context mContext;
      private double latitude;
      private double longitude;
  
      public LocationHelper(Context mContext) {
          this.mContext = mContext;
          location_ajax();
      }
  
      public double getLatitude() {
          return latitude;
      }
  
      public double getLongitude() {
          return longitude;
      }
  
      private void location_ajax() {
          // Get the location
          LocationAjaxCallback cb = new LocationAjaxCallback();
          cb.weakHandler(this, "locationCb");
          cb.async(mContext);
      }
  
      public void locationCb(String url, Location loc, AjaxStatus status) {
          if (loc != null) {
              //Toast.makeText(mContext, loc.toString(), Toast.LENGTH_LONG).show();
              this.latitude = loc.getLatitude();
              this.longitude = loc.getLongitude();            
          } else {
              Toast.makeText(mContext, mContext.getString(R.string.msg_could_not_retrieve_location), Toast.LENGTH_LONG).show();
          }
      }   
  }
  </code></pre>
  
  <p>Logcat:</p>
  
  <pre><code>03-19 23:03:17.146: W/AQuery(29954): reporting:java.lang.reflect.InvocationTargetException
  03-19 23:03:17.146: W/AQuery(29954):    at java.lang.reflect.Method.invokeNative(Native Method)
  03-19 23:03:17.146: W/AQuery(29954):    at java.lang.reflect.Method.invoke(Method.java:511)
  03-19 23:03:17.146: W/AQuery(29954):    at com.androidquery.util.AQUtility.invokeMethod(AQUtility.java:206)
  03-19 23:03:17.146: W/AQuery(29954):    at com.androidquery.util.AQUtility.invokeHandler(AQUtility.java:183)
  03-19 23:03:17.146: W/AQuery(29954):    at com.androidquery.callback.AbstractAjaxCallback.callback(AbstractAjaxCallback.java:577)
  03-19 23:03:17.146: W/AQuery(29954):    at com.androidquery.callback.LocationAjaxCallback.failure(LocationAjaxCallback.java:192)
  03-19 23:03:17.146: W/AQuery(29954):    at com.androidquery.callback.LocationAjaxCallback.access$3(LocationAjaxCallback.java:183)
  03-19 23:03:17.146: W/AQuery(29954):    at com.androidquery.callback.LocationAjaxCallback$Listener.run(LocationAjaxCallback.java:308)
  03-19 23:03:17.146: W/AQuery(29954):    at java.util.Timer$TimerImpl.run(Timer.java:284)
  03-19 23:03:17.146: W/AQuery(29954): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
  03-19 23:03:17.146: W/AQuery(29954):    at android.os.Handler.&lt;init&gt;(Handler.java:121)
  03-19 23:03:17.146: W/AQuery(29954):    at android.widget.Toast$TN.&lt;init&gt;(Toast.java:361)
  03-19 23:03:17.146: W/AQuery(29954):    at android.widget.Toast.&lt;init&gt;(Toast.java:97)
  03-19 23:03:17.146: W/AQuery(29954):    at android.widget.Toast.makeText(Toast.java:254)
  03-19 23:03:17.146: W/AQuery(29954):    at uk.ac.gla.project.helpers.LocationHelper.locationCb(LocationHelper.java:46)
  03-19 23:03:17.146: W/AQuery(29954):    ... 9 more
  03-19 23:03:20.001: W/AQuery(29954): reporting:java.lang.reflect.InvocationTargetException
  03-19 23:03:20.001: W/AQuery(29954):    at java.lang.reflect.Method.invokeNative(Native Method)
  03-19 23:03:20.001: W/AQuery(29954):    at java.lang.reflect.Method.invoke(Method.java:511)
  03-19 23:03:20.001: W/AQuery(29954):    at com.androidquery.util.AQUtility.invokeMethod(AQUtility.java:206)
  03-19 23:03:20.001: W/AQuery(29954):    at com.androidquery.util.AQUtility.invokeHandler(AQUtility.java:183)
  03-19 23:03:20.001: W/AQuery(29954):    at com.androidquery.callback.AbstractAjaxCallback.callback(AbstractAjaxCallback.java:577)
  03-19 23:03:20.001: W/AQuery(29954):    at com.androidquery.callback.LocationAjaxCallback.failure(LocationAjaxCallback.java:192)
  03-19 23:03:20.001: W/AQuery(29954):    at com.androidquery.callback.LocationAjaxCallback.access$3(LocationAjaxCallback.java:183)
  03-19 23:03:20.001: W/AQuery(29954):    at com.androidquery.callback.LocationAjaxCallback$Listener.run(LocationAjaxCallback.java:308)
  03-19 23:03:20.001: W/AQuery(29954):    at java.util.Timer$TimerImpl.run(Timer.java:284)
  03-19 23:03:20.001: W/AQuery(29954): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
  03-19 23:03:20.001: W/AQuery(29954):    at android.os.Handler.&lt;init&gt;(Handler.java:121)
  03-19 23:03:20.001: W/AQuery(29954):    at android.widget.Toast$TN.&lt;init&gt;(Toast.java:361)
  03-19 23:03:20.001: W/AQuery(29954):    at android.widget.Toast.&lt;init&gt;(Toast.java:97)
  03-19 23:03:20.001: W/AQuery(29954):    at android.widget.Toast.makeText(Toast.java:254)
  03-19 23:03:20.001: W/AQuery(29954):    at uk.ac.gla.project.helpers.LocationHelper.locationCb(LocationHelper.java:46)
  03-19 23:03:20.001: W/AQuery(29954):    ... 9 more
  03-19 23:03:20.671: W/AQuery(29954): reporting:java.lang.reflect.InvocationTargetException
  03-19 23:03:20.671: W/AQuery(29954):    at java.lang.reflect.Method.invokeNative(Native Method)
  03-19 23:03:20.671: W/AQuery(29954):    at java.lang.reflect.Method.invoke(Method.java:511)
  03-19 23:03:20.671: W/AQuery(29954):    at com.androidquery.util.AQUtility.invokeMethod(AQUtility.java:206)
  03-19 23:03:20.671: W/AQuery(29954):    at com.androidquery.util.AQUtility.invokeHandler(AQUtility.java:183)
  03-19 23:03:20.671: W/AQuery(29954):    at com.androidquery.callback.AbstractAjaxCallback.callback(AbstractAjaxCallback.java:577)
  03-19 23:03:20.671: W/AQuery(29954):    at com.androidquery.callback.LocationAjaxCallback.failure(LocationAjaxCallback.java:192)
  03-19 23:03:20.671: W/AQuery(29954):    at com.androidquery.callback.LocationAjaxCallback.access$3(LocationAjaxCallback.java:183)
  03-19 23:03:20.671: W/AQuery(29954):    at com.androidquery.callback.LocationAjaxCallback$Listener.run(LocationAjaxCallback.java:308)
  03-19 23:03:20.671: W/AQuery(29954):    at java.util.Timer$TimerImpl.run(Timer.java:284)
  03-19 23:03:20.671: W/AQuery(29954): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
  03-19 23:03:20.671: W/AQuery(29954):    at android.os.Handler.&lt;init&gt;(Handler.java:121)
  03-19 23:03:20.671: W/AQuery(29954):    at android.widget.Toast$TN.&lt;init&gt;(Toast.java:361)
  03-19 23:03:20.671: W/AQuery(29954):    at android.widget.Toast.&lt;init&gt;(Toast.java:97)
  03-19 23:03:20.671: W/AQuery(29954):    at android.widget.Toast.makeText(Toast.java:254)
  03-19 23:03:20.671: W/AQuery(29954):    at uk.ac.gla.project.helpers.LocationHelper.locationCb(LocationHelper.java:46)
  03-19 23:03:20.671: W/AQuery(29954):    ... 9 more
  03-19 23:03:20.906: W/AQuery(29954): reporting:java.lang.reflect.InvocationTargetException
  03-19 23:03:20.906: W/AQuery(29954):    at java.lang.reflect.Method.invokeNative(Native Method)
  03-19 23:03:20.906: W/AQuery(29954):    at java.lang.reflect.Method.invoke(Method.java:511)
  03-19 23:03:20.906: W/AQuery(29954):    at com.androidquery.util.AQUtility.invokeMethod(AQUtility.java:206)
  03-19 23:03:20.906: W/AQuery(29954):    at com.androidquery.util.AQUtility.invokeHandler(AQUtility.java:183)
  03-19 23:03:20.906: W/AQuery(29954):    at com.androidquery.callback.AbstractAjaxCallback.callback(AbstractAjaxCallback.java:577)
  03-19 23:03:20.906: W/AQuery(29954):    at com.androidquery.callback.LocationAjaxCallback.failure(LocationAjaxCallback.java:192)
  03-19 23:03:20.906: W/AQuery(29954):    at com.androidquery.callback.LocationAjaxCallback.access$3(LocationAjaxCallback.java:183)
  03-19 23:03:20.906: W/AQuery(29954):    at com.androidquery.callback.LocationAjaxCallback$Listener.run(LocationAjaxCallback.java:308)
  03-19 23:03:20.906: W/AQuery(29954):    at java.util.Timer$TimerImpl.run(Timer.java:284)
  03-19 23:03:20.906: W/AQuery(29954): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
  03-19 23:03:20.906: W/AQuery(29954):    at android.os.Handler.&lt;init&gt;(Handler.java:121)
  03-19 23:03:20.906: W/AQuery(29954):    at android.widget.Toast$TN.&lt;init&gt;(Toast.java:361)
  03-19 23:03:20.906: W/AQuery(29954):    at android.widget.Toast.&lt;init&gt;(Toast.java:97)
  03-19 23:03:20.906: W/AQuery(29954):    at android.widget.Toast.makeText(Toast.java:254)
  03-19 23:03:20.906: W/AQuery(29954):    at uk.ac.gla.project.helpers.LocationHelper.locationCb(LocationHelper.java:46)
  03-19 23:03:20.906: W/AQuery(29954):    ... 9 more
  03-19 23:03:21.061: W/AQuery(29954): reporting:java.lang.reflect.InvocationTargetException
  03-19 23:03:21.061: W/AQuery(29954):    at java.lang.reflect.Method.invokeNative(Native Method)
  03-19 23:03:21.061: W/AQuery(29954):    at java.lang.reflect.Method.invoke(Method.java:511)
  03-19 23:03:21.061: W/AQuery(29954):    at com.androidquery.util.AQUtility.invokeMethod(AQUtility.java:206)
  03-19 23:03:21.061: W/AQuery(29954):    at com.androidquery.util.AQUtility.invokeHandler(AQUtility.java:183)
  03-19 23:03:21.061: W/AQuery(29954):    at com.androidquery.callback.AbstractAjaxCallback.callback(AbstractAjaxCallback.java:577)
  03-19 23:03:21.061: W/AQuery(29954):    at com.androidquery.callback.LocationAjaxCallback.failure(LocationAjaxCallback.java:192)
  03-19 23:03:21.061: W/AQuery(29954):    at com.androidquery.callback.LocationAjaxCallback.access$3(LocationAjaxCallback.java:183)
  03-19 23:03:21.061: W/AQuery(29954):    at com.androidquery.callback.LocationAjaxCallback$Listener.run(LocationAjaxCallback.java:308)
  03-19 23:03:21.061: W/AQuery(29954):    at java.util.Timer$TimerImpl.run(Timer.java:284)
  03-19 23:03:21.061: W/AQuery(29954): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
  03-19 23:03:21.061: W/AQuery(29954):    at android.os.Handler.&lt;init&gt;(Handler.java:121)
  03-19 23:03:21.061: W/AQuery(29954):    at android.widget.Toast$TN.&lt;init&gt;(Toast.java:361)
  03-19 23:03:21.061: W/AQuery(29954):    at android.widget.Toast.&lt;init&gt;(Toast.java:97)
  03-19 23:03:21.061: W/AQuery(29954):    at android.widget.Toast.makeText(Toast.java:254)
  03-19 23:03:21.061: W/AQuery(29954):    at uk.ac.gla.project.helpers.LocationHelper.locationCb(LocationHelper.java:46)
  03-19 23:03:21.061: W/AQuery(29954):    ... 9 more
  03-19 23:03:21.486: W/AQuery(29954): reporting:java.lang.reflect.InvocationTargetException
  03-19 23:03:21.486: W/AQuery(29954):    at java.lang.reflect.Method.invokeNative(Native Method)
  03-19 23:03:21.486: W/AQuery(29954):    at java.lang.reflect.Method.invoke(Method.java:511)
  03-19 23:03:21.486: W/AQuery(29954):    at com.androidquery.util.AQUtility.invokeMethod(AQUtility.java:206)
  03-19 23:03:21.486: W/AQuery(29954):    at com.androidquery.util.AQUtility.invokeHandler(AQUtility.java:183)
  03-19 23:03:21.486: W/AQuery(29954):    at com.androidquery.callback.AbstractAjaxCallback.callback(AbstractAjaxCallback.java:577)
  03-19 23:03:21.486: W/AQuery(29954):    at com.androidquery.callback.LocationAjaxCallback.failure(LocationAjaxCallback.java:192)
  03-19 23:03:21.486: W/AQuery(29954):    at com.androidquery.callback.LocationAjaxCallback.access$3(LocationAjaxCallback.java:183)
  03-19 23:03:21.486: W/AQuery(29954):    at com.androidquery.callback.LocationAjaxCallback$Listener.run(LocationAjaxCallback.java:308)
  03-19 23:03:21.486: W/AQuery(29954):    at java.util.Timer$TimerImpl.run(Timer.java:284)
  03-19 23:03:21.486: W/AQuery(29954): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
  03-19 23:03:21.486: W/AQuery(29954):    at android.os.Handler.&lt;init&gt;(Handler.java:121)
  03-19 23:03:21.486: W/AQuery(29954):    at android.widget.Toast$TN.&lt;init&gt;(Toast.java:361)
  03-19 23:03:21.486: W/AQuery(29954):    at android.widget.Toast.&lt;init&gt;(Toast.java:97)
  03-19 23:03:21.486: W/AQuery(29954):    at android.widget.Toast.makeText(Toast.java:254)
  03-19 23:03:21.486: W/AQuery(29954):    at uk.ac.gla.project.helpers.LocationHelper.locationCb(LocationHelper.java:46)
  </code></pre>
  
  <p>Are there any reliable alternatives?</p>