I've got a similar problem to the question asked here: Sending message to a Handler on a dead thread when getting a location from an IntentService
I've put a LocationListener in an IntentService, which I hear is a bad place to put it since the listener gets destroyed after the onHandleIntent()
completes.
My intent service checks the last known location based on GPS and Network provider. I'm looking for a way to "wake up" the GPS and Network locator before I check where the user is at.
The IntentService is run every minute, and it checks for an updated location every 5-30 minutes. Is there a way to setup a requestLocationUpdates
that I can call prior to the actual location check?
12-31 14:09:33.664: W/MessageQueue(3264): Handler (android.location.LocationManager$ListenerTransport$1) {41403330} sending message to a Handler on a dead thread
12-31 14:09:33.664: W/MessageQueue(3264): java.lang.RuntimeException: Handler (android.location.LocationManager$ListenerTransport$1) {41403330} sending message to a Handler on a dead thread
12-31 14:09:33.664: W/MessageQueue(3264): at android.os.MessageQueue.enqueueMessage(MessageQueue.java:196)
12-31 14:09:33.664: W/MessageQueue(3264): at android.os.Handler.sendMessageAtTime(Handler.java:473)
12-31 14:09:33.664: W/MessageQueue(3264): at android.os.Handler.sendMessageDelayed(Handler.java:446)
12-31 14:09:33.664: W/MessageQueue(3264): at android.os.Handler.sendMessage(Handler.java:383)
12-31 14:09:33.664: W/MessageQueue(3264): at android.location.LocationManager$ListenerTransport.onLocationChanged(LocationManager.java:193)
12-31 14:09:33.664: W/MessageQueue(3264): at android.location.ILocationListener$Stub.onTransact(ILocationListener.java:58)
12-31 14:09:33.664: W/MessageQueue(3264): at android.os.Binder.execTransact(Binder.java:338)
12-31 14:09:33.664: W/MessageQueue(3264): at dalvik.system.NativeStart.run(Native Method)
ScheduledService
public class ScheduledService extends IntentService {
private LocationManager lm;
LocationListener locationListener;
public ScheduledService() {
super("ScheduledService");
}
@Override
protected void onHandleIntent(Intent intent) {
lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
lm.removeUpdates(this);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
if(isNetworkAvailable()) {
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
try {
Location lastKnownLocation = getBestLocation();
double lat = lastKnownLocation.getLatitude();
double lon = lastKnownLocation.getLongitude();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}