Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid HandlerThread stops working when device unplugged from USB cable
    text
    copied!<p>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:</p> <pre><code>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); // &lt;-- Testing value. Will be much larger. ... } </code></pre> <p>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:</p> <ul> <li>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.</li> <li>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.</li> </ul> <p>Things to note:</p> <ul> <li>In either case above, if I plug the USB cable back in, it instantly starts working again.</li> <li>In either case above, Settings -> Applications -> Running Services always indicates that my <strong>Service is still running</strong>.</li> <li>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. </li> </ul> <p>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. </p>
 

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