Sending message to a Handler on a dead thread when getting a location from an IntentService
Asked Answered
T

3

36

My app needs location fixes on regular basis, even when the phone is not awake. To do this, I am using IntentService with the pattern generously provided by Commonsware. https://github.com/commonsguy/cwac-wakeful

To get location fixes, I rely on the following code provided by a member called Fedor. What is the simplest and most robust way to get the user's current location on Android?. I slightly modified it to return null instead of getting the last known location

They both work perfectly fine when not combined: I can get a location fix from Fedor's class in an Activity, and I can do some basic stuff ( like logging something) using Commonsware class.

The problem occurs when I am trying to get location fixes from the Commonsware's WakefulIntentService subclass. The LocationListeners do not react to locationUpdates and I get these warnings (I don't get yet the subtleties of threads, handlers, ...) . Can someone help me understand what the problem is? Thanks and I wish you all a very happy new year :)

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)

This is my WakefulIntentService subclass:

public class AppService extends WakefulIntentService {
public static final String TAG = "AppService";
public AppService() {
    super("AppService");
}

public LocationResult locationResult = new LocationResult() {
    @Override
    public void gotLocation(final Location location) {
        if (location == null)
            Log.d(TAG, "location could not be retrieved");
        else
            sendMessage(location);
    }
};

@Override
protected void doWakefulWork(Intent intent) {
    PhoneLocation myLocation;
    myLocation = new PhoneLocation();
    myLocation.getLocation(getApplicationContext(), locationResult);
}

And here a copy of Fedor's class (slightly modified: no need of GPS nor last known location)

public class PhoneLocation {
public static String TAG = "MyLocation";
Timer timer1;
LocationManager lm;
LocationResult locationResult;
boolean gps_enabled=false;
boolean network_enabled=false;

public boolean getLocation(Context context, LocationResult result)
{
    //I use LocationResult callback class to pass location value from MyLocation to user code.
    locationResult=result;
    if(lm==null) lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

    //exceptions will be thrown if provider is not permitted.
    try{gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){}
    try{network_enabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}catch(Exception ex){}

    //don't start listeners if no provider is enabled
    if(!gps_enabled && !network_enabled)
        return false;

     if(network_enabled)  lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);

    timer1=new Timer();
    timer1.schedule(new ReturnNullLocation(), 20000);
    return true;
}

 LocationListener locationListenerNetwork = new LocationListener() {
     public void onLocationChanged(Location location) {
        timer1.cancel();
        locationResult.gotLocation(location);
        lm.removeUpdates(this);
    }
    public void onProviderDisabled(String provider) {}
    public void onProviderEnabled(String provider) {}
    public void onStatusChanged(String provider, int status, Bundle extras) {}
};

class ReturnNullLocation extends TimerTask {
    @Override
    public void run() { 
          lm.removeUpdates(locationListenerNetwork);
          locationResult.gotLocation(null);
    }
}

public static abstract class LocationResult{
    public abstract void gotLocation(Location location);

}

}

Thankful answered 31/12, 2011 at 19:45 Comment(0)
L
35

You cannot safely register listeners from an IntentService, as the IntentService goes away as soon as onHandleIntent() (a.k.a., doWakefulWork()) completes. Instead, you will need to use a regular service, plus handle details like timeouts (e.g., the user is in a basement and cannot get a GPS signal).

Libby answered 31/12, 2011 at 20:9 Comment(10)
It works like a charm! Thank you so much! I have set up the alarm in my Application subclass and created startLocationPoll() and stopLocationPoll() methods there. Happy new year!Thankful
when I use the example on the link.... i got ... "E/LocationPoller( 442): Invalid Intent -- has no provider" ... any clue please?Knelt
@user836026: You failed to provide the LocationPoller.EXTRA_PROVIDER extra on the Intent, indicating which provider to use. If you have additional questions on this component, please use the "Ask Question" button, rather than posting comments here.Libby
@CommonsWare:thanks ... "Invalid Intent -- has no provide" message disappeared, however, im trying to put LocationReceiver as android.intent.action.BOOT_COMPLETED intent-filter, but it's not working. How could I make LocationReceiver works on boot.Knelt
Sorry,,, I meant LocationReceiver, not LocationReceiver.Knelt
notice, I'm using simulator .. not real handset in case it make difference.Knelt
@Libby i am using your component and i'm still getting the "handler on a dead thread" message... only thing i changed was to requestLocationUpdates on all enabled providers.Laudian
@johnellis: The LocationPoller cited in this answer was discontinued a year or two ago. There was at least one fork of it (linked to from the project's home page); I do not know if that is being maintained or not.Libby
it seems to be working for me... i just added an Override of "quit" on the Thread and in there, removed the location listener... that did the trickLaudian
@CodeShadow: That library has long since been discontinued. I have removed it from the answer.Libby
G
12

I've had a similar situation, and I've used the android Looper facility to good effect: http://developer.android.com/reference/android/os/Looper.html

concretely, just after calling the function setting up the the listener, I call:

Looper.loop();

That blocks the current thread so it doesn't die but at the same time lets it process events, so you'll have a chance to get notified when your listener is called. A simple loop would prevent you from getting notified when the listener is called.

And when the listener is called and I'm done processing its data, I put:

Looper.myLooper().quit();
Galarza answered 23/10, 2012 at 13:48 Comment(2)
I do the same. But I add if(Looper.myLooper() == null) Looper.prepare(); before Looper.loop();Fearful
I also run a separate thread to timeout after some specific time (5-15 secs for network, 45-120 secs for gps). Because otherwise Looper.loop() will keep the device awake forever.Fearful
D
6

For others like me: I had a similar problem related to AsyncTask. As I understand, that class assumes that it is initialized on the UI (main) thread.

A workaround (one of several possible) is to place the following in MyApplication class:

@Override
public void onCreate() {
    // workaround for http://code.google.com/p/android/issues/detail?id=20915
    try {
        Class.forName("android.os.AsyncTask");
    } catch (ClassNotFoundException e) {}
    super.onCreate();
}

(do not forget to mention it in AndroidManifest.xml:

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:name="MyApplication"
    >

)

Delftware answered 25/4, 2012 at 10:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.