Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
240 views
in Technique[技术] by (71.8m points)

multithreading - 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:

  • If I start the app from Eclipse (either debug or run), things work fine until I unplug the USB cable, at which point my HandlerThread seems to stop.
  • If I start the app from the phone itself, after disconnecting the USB cable, the service starts up but the thread doesn't seem to be running.

Things to note:

  • In either case above, if I plug the USB cable back in, it instantly starts working again.
  • In either case above, Settings -> Applications -> Running Services always indicates that my Service is still running.
  • I've sprinkled some debug Toasts around; as far as I can tell my Service is not being destroyed, but the HandlerThread's message queue ceases to process messages.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...