带有 locationListener 回调的 Android 服务

发布于 2024-09-24 15:28:08 字数 667 浏览 4 评论 0原文

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

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

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

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

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

另外,lastKnownlocation 在什么时候更新?每次 GPS 记录新位置时;它会更新吗?我想知道的是如果不是最新的还能关闭到最新吗?因为我没有在 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:

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

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

狼性发作 2024-10-01 15:28:08

但是如果我现在确实想要在服务而不是活动中获得最新位置该怎么办?

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

如何等待回调发生并停止我的服务返回。

你不知道。你按照你说过的去做:

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

因此,让您的 Service (希望是 IntentService)检查是否 getLastKnownLocation()< /code> 恰好有一个值。如果有,请使用它。否则,registerLocationUpdates() 使用 PendingIntent 将控制权传递回您的 IntentService。当您得到那个 意图时,使用该位置并取消注册更新(假设警报周期很好而且很长,例如每小时一次)。

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

此外,lastKnownlocation 在什么时候更新?每次 GPS 记录新位置时;是吗>更新了吗?

据我所知,是的。

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

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:

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

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

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.

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

AFAIK, yes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文