我有一个安卓应用程序.根据用户当前的地理位置,我想在后台获取一些远程数据并存储.我的实现是:
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 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!