<legend id='TGiPu'><style id='TGiPu'><dir id='TGiPu'><q id='TGiPu'></q></dir></style></legend>

    <i id='TGiPu'><tr id='TGiPu'><dt id='TGiPu'><q id='TGiPu'><span id='TGiPu'><b id='TGiPu'><form id='TGiPu'><ins id='TGiPu'></ins><ul id='TGiPu'></ul><sub id='TGiPu'></sub></form><legend id='TGiPu'></legend><bdo id='TGiPu'><pre id='TGiPu'><center id='TGiPu'></center></pre></bdo></b><th id='TGiPu'></th></span></q></dt></tr></i><div id='TGiPu'><tfoot id='TGiPu'></tfoot><dl id='TGiPu'><fieldset id='TGiPu'></fieldset></dl></div>

        <bdo id='TGiPu'></bdo><ul id='TGiPu'></ul>
    1. <tfoot id='TGiPu'></tfoot>
    2. <small id='TGiPu'></small><noframes id='TGiPu'>

    3. 带有 locationListener 回调的 Android 服务

      时间:2023-09-01

      • <tfoot id='7JYN3'></tfoot>

        <legend id='7JYN3'><style id='7JYN3'><dir id='7JYN3'><q id='7JYN3'></q></dir></style></legend>

          <small id='7JYN3'></small><noframes id='7JYN3'>

            <bdo id='7JYN3'></bdo><ul id='7JYN3'></ul>

                <tbody id='7JYN3'></tbody>
              1. <i id='7JYN3'><tr id='7JYN3'><dt id='7JYN3'><q id='7JYN3'><span id='7JYN3'><b id='7JYN3'><form id='7JYN3'><ins id='7JYN3'></ins><ul id='7JYN3'></ul><sub id='7JYN3'></sub></form><legend id='7JYN3'></legend><bdo id='7JYN3'><pre id='7JYN3'><center id='7JYN3'></center></pre></bdo></b><th id='7JYN3'></th></span></q></dt></tr></i><div id='7JYN3'><tfoot id='7JYN3'></tfoot><dl id='7JYN3'><fieldset id='7JYN3'></fieldset></dl></div>
                本文介绍了带有 locationListener 回调的 Android 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我有一个安卓应用程序.根据用户当前的地理位置,我想在后台获取一些远程数据并存储.我的实现是:

                I have an android application. Based on the current geo location of user, I want to fetch some remote data in background and store it. My implementation is:

                在特定的时间间隔,警报会启动我的服务.服务使用匿名类查询当前位置并注册一个 locationListener 回调.在调用 onLocationChanged() 时,我启动从服务器获取远程数据.

                At specific interval a alarm fires up my service. Service uses an anonymous class to query current location and registers a locationListener callback. On call of onLocationChanged() I initiate the remote data fetch from server.

                但是,一旦我的服务完成使用匿名类注册位置侦听器,它就会按预期返回;因为它不会在完成之前等待回调发生.由于回调需要一些时间并在服务已经返回时进行调用,所以它会抛出一个错误:

                However once my service is done registering the location listener using anonymos class, it returns as expected; as it doesn't wait for callback to happen before finishing. Since callback takes some time and makes a call when service has already returned, it throws an error saying:

                java.lang.RuntimeException: Handler{43e82510} sending message to a Handler on a dead thread
                

                这很好理解.现在对我来说一个快速的解决方法是我可以使用 locationManager 中的 getLastKnownLocation ,因为它不会通过回调响应;但是如果我现在确实想要最新的位置,在服务中而不是活动中怎么办?如何等待回调发生并停止我的服务返回.

                Which is quite understandable. One quick workaround for me now is that I can use getLastKnownLocation from locationManager as that doesn't respond back by callback; but what if I do want the latest location right now, in a service and not activity? How can I wait for callback to happen and stop my service from returning.

                另外,lastKnownlocation 什么时候更新?每次 GPS 注册一个新位置时;它会更新吗?我想知道的是,如果它不是最新的,它仍然可以关闭到最新吗?因为我在 android 模拟器中没有看到配置后续更新之间的时间段的选项.

                Also, at what point does lastKnownlocation gets updated? Everytime GPS registers a new location; does it update it? What I want to know is that if it's not latest can it still be closed to latest? As I didn't see an option in android emulator to configure the time period between subsequent updates.

                非常感谢任何帮助.干杯

                Any help is much appreciated. Cheers

                推荐答案

                但是如果我现在确实想要最新的位置,在服务中而不是在活动中呢?

                but what if I do want the latest location right now, in a service and not activity?

                抱歉,这在服务或活动中都是不可能的.例如,如果 GPS 无线电已关闭,而您正在从 GPS 请求位置数据,则需要数十秒才能得到修复,如果您很幸运的话.它可能根本无法修复.

                Sorry, but that is not possible, in either a service or an activity. For example, if the GPS radio is off, and you are requesting location data from GPS, it will take tens of seconds just to get a fix, and that's if you are lucky. It might not get a fix at all.

                如何等待回调发生并阻止我的服务返回.

                How can I wait for callback to happen and stop my service from returning.

                你没有.你按照你说的去做:

                You don't. You do what you said you would do:

                使用 locationManager 中的 getLastKnownLocation,因为它不会通过回调响应

                use getLastKnownLocation from locationManager as that doesn't respond back by callback

                所以,让您的 Service(希望是 IntentService)检查 getLastKnownLocation() 是否恰好有一个值.如果是,请使用它.否则,registerLocationUpdates() 使用 PendingIntent 将控制权交还给您的 IntentService.当你得到那个 Intent时,使用该位置并取消注册更新(假设警报周期很长,比如每小时一次).

                So, have your Service (which is hopefully an IntentService) check to see if getLastKnownLocation() happens to have a value. If it does, use it. Otherwise, registerLocationUpdates() using a PendingIntent that will pass control back to your IntentService. When you get that Intent, use the location and unregister for updates (assuming the alarm period is nice and long, like, say, once an hour).

                如果您的闹钟是 _WAKEUP 闹钟,事情就会变得棘手.然后,您需要按住 WakeLock,以便在您尝试 GPS 定位时设备不会重新进入睡眠状态.但是,您需要在某个时候释放该 WakeLock,如果我们无法获得 GPS 定位...嗯...嗯,这就是棘手的部分.试图找出一种干净的方式来处理这个问题,并将其实现为可重用的组件(例如,LocationAlarmService),这是我的待办事项列表中的 18,000 项之一.

                Things get tricky if your alarm is a _WAKEUP alarm. You will then need to hold a WakeLock, so the device does not fall back asleep while you are trying to get a GPS fix. However, you need to release that WakeLock sometime, and if we cannot get a GPS fix...ummm...well, that's the tricky part. Trying to figure out a nice clean way of handling this, and implementing it as a reusable component (e.g., LocationAlarmService), is one of 18,000 items on my to-do list.

                另外,lastKnownlocation 什么时候更新?每次 GPS 注册一个新位置时;它 > 更新了吗?

                Also, at what point does lastKnownlocation gets updated? Everytime GPS registers a new location; does it > update it?

                AFAIK,是的.

                这篇关于带有 locationListener 回调的 Android 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:地理定位在 iOS 8 模拟器中不起作用 下一篇:第一次 MKReverseGeocoder: didFailWithError:Error Domain=NS

                相关文章

                最新文章

                  <bdo id='vFT2g'></bdo><ul id='vFT2g'></ul>

                1. <small id='vFT2g'></small><noframes id='vFT2g'>

                2. <i id='vFT2g'><tr id='vFT2g'><dt id='vFT2g'><q id='vFT2g'><span id='vFT2g'><b id='vFT2g'><form id='vFT2g'><ins id='vFT2g'></ins><ul id='vFT2g'></ul><sub id='vFT2g'></sub></form><legend id='vFT2g'></legend><bdo id='vFT2g'><pre id='vFT2g'><center id='vFT2g'></center></pre></bdo></b><th id='vFT2g'></th></span></q></dt></tr></i><div id='vFT2g'><tfoot id='vFT2g'></tfoot><dl id='vFT2g'><fieldset id='vFT2g'></fieldset></dl></div>
                  <legend id='vFT2g'><style id='vFT2g'><dir id='vFT2g'><q id='vFT2g'></q></dir></style></legend>

                    <tfoot id='vFT2g'></tfoot>