sleep
sleep

Reputation: 4954

Android HandlerThread stops working when device unplugged from USB cable

My application includes a Service that spawns a HandlerThread that periodically requests a location update from the LocationManager. Each time I receive a location updated I disable location updates, and send a delayed message to the Hander that will start updates again in the future:

public class VMLocator extends HandlerThread implements LocationListener {

 ...

private final class VMHandler extends Handler
{
    public VMHandler(Looper looper)
    {
        super(looper);
    }

    @Override
    public void handleMessage(Message msg) 
    {
        if(MSG_START_LOCATION_UPDATES == msg.what)
        {
            startLocationUpdates();
        }
    }

}

...

@Override
public void onLocationChanged(Location location) {
    ...
    stopLocationUpdates();
    // Schedule updates to start again in the future.
    Message msg = new Message();
    msg.what = MSG_START_LOCATION_UPDATES;
    handler.sendMessageDelayed(msg, 5000);   // <-- Testing value. Will be much larger.
    ...
}

I'm currently testing using a HTC Desire S handset running 2.3.3, developing with Eclipse. Everything works fine while the phone is connected via the USB cable to my development machine. However:

Things to note:

I've tried running from the phone while disconnected and with USB debugging disabled with the same result. I'm sensing that there is something simple in the docs I've missed, because I thought that running/debugging from Eclipse installed the app, and the app should function normally regardless of whether the USB cable is plugged in or not.

Upvotes: 0

Views: 1886

Answers (1)

sleep
sleep

Reputation: 4954

Thanks dragonroot, your suggestion helped me realise what the problem was.

The issue was that I had a call to Debug.waitForDebugger(); in the HandlerThread. As soon as the USB cable is detached this call stalls forever, since no debugger connection can be found.

A very basic mistake in hindsight, hopefully this will help someone else avoid it.

Upvotes: 2

Related Questions