Android的HandlerThread停止工作,当设备从拔出USB电缆 [英] Android HandlerThread stops working when device unplugged from USB cable

查看:140
本文介绍了Android的HandlerThread停止工作,当设备从拔出USB电缆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序包括派生周期性请求来自的LocationManager位置更新一HandlerThread服务。每次我收到的位置更新一次我禁用位置更新,并发送延迟的消息,这将在未来再次启动更新左撇子:

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.
    ...
}

我目前正在测试使用的是HTC Desire S系列手机运行2.3.3,使用Eclipse开发。而手机通过USB线连接到我的机器一切正常。但是:

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:


  • 如果我开始在Eclipse(调试或运行)的应用程序,一切工作正常,直到我拔掉USB连接线,此时我HandlerThread似乎停止了。

  • 如果我开始从手机本身的应用程序,断开USB电缆后,该服务启动但该线程似乎没有运行。

注意事项:


  • 在上述两种情况下,如果我再插上USB电缆,它会立即开始工作了。

  • 在这两种情况下上面,设置 - >应用程序 - >运行的服务总是表示我的服务仍在运行

  • 我周围撒一些调试祝酒​​词;据我可以告诉我的服务没有被破坏,但HandlerThread的消息队列停止处理消息。

我试图从手机运行时断开连接,以及带USB调试具有相同的结果无效。我感觉到,有东西在我已经错过了文档简单,因为我认为从Eclipse中运行/调试安装的应用程序,该应用程序应该无论USB电缆是否或没有插入正常工作。

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.

推荐答案

感谢dragonroot,你的建议使我意识到,问题是什么。

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

问题是,我不得不通话Debug.waitForDebugger(); 在HandlerThread。一旦USB电缆脱落永远此调用摊位,因为没有调试器连接,都可以找到。

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.

这篇关于Android的HandlerThread停止工作,当设备从拔出USB电缆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆