Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid onLocationChanged not called
    text
    copied!<p>What do I need to do to get my LocationListener to fire it's onLocationChanged method?</p> <p>I am new to this stuff (java and android). Here is what I am doing. I handle a request for a location by calling context.startService(serviceIntent); Inside the intent service i have a nested class to act as the LocationListener in side the onHandleIntent method i start a thread which requests the location updates I maintain a reference to the LocationListener object and the tread in the the OnHandleIntent method.</p> <p>my plan was for the onLocationChanged method to update a variable that i could check periodically to see if the update has been completed, then break out of loop and do something with the location.</p> <p>The problem is the onLocationChanged method never gets called. The LocationListener object is still valid right to the end of the onHandleIntent method. I can even call it directly and get the debug message that I am expecting. I am pretty sure that the DDMS is changing the location as I have printed it out in various debug logs and i see it changing.</p> <p>Here is the code:</p> <pre><code>package com.example.locationrequestreceiver; import android.app.IntentService; import android.content.Context; import android.content.Intent; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.os.Looper; import android.util.Log; public class LocationRequestService extends IntentService { private LocationManager locationManager; public class UpdateHandler implements Runnable, LocationListener { @Override public void onLocationChanged(Location arg0) { Log.d("myStuff", "inside location changed"); } @Override public void onProviderEnabled(String provider) {} @Override public void onStatusChanged(String provider, int status, Bundle extras) {} @Override public void onProviderDisabled(String provider) {} @Override public void run() { // TODO Auto-generated method stub Looper.prepare(); Log.d("myStuff", "inside thread"); locationManager.requestLocationUpdates("gps", 0, 0, this); Log.d("myStuff", "run ending"); Looper.loop() // &lt;--- this was the fix } } public LocationRequestService() { super("LocationRequrestService"); Log.d("myStuff", "inside the service"); } @Override protected void onHandleIntent(Intent arg0) { locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); UpdateHandler update_handler = new UpdateHandler(); Thread update_handler_thread = new Thread(update_handler); Log.d("myStuff", "starting thread"); update_handler_thread.start(); for (int count = 0; count &lt; 20; count++) { try { Thread.sleep(1000); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } } Location location = locationManager.getLastKnownLocation("gps"); update_handler.onLocationChanged(location); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload