android:处理可选位置

发布于 2024-09-30 04:36:57 字数 539 浏览 0 评论 0原文

我有一个 Android 应用程序,可以在有或没有位置的情况下运行。在活动中处理这种情况很简单:我设置一个位置侦听器并等待进度对话框,但允许用户中止等待(碰巧如果禁用位置提供程序,则不会出现任何特殊错误返回位置...很好)。

但是,我还有一项需要位置的服务。该服务可能在活动之前运行,因此我不能依赖活动来获取(或不)位置。

问题是如何在服务中处理这种情况。我可以等待该位置,但由于我不能依赖用户中止,我如何知道何时停止等待,并假设没有可用的位置?看来我可以做类似的事情,

  1. 用“获取位置”操作安排服务,设置位置侦听器,等待更新,安排相同的服务,比如3分钟后,
  2. 在服务操作“刷新”中使用“刷新”操作,获取最后一个已知位置,并对此感到满意。它要么被获得,要么未被获得。如果它为空,我们就知道我们没有位置,并且我们已经尽力了。

看起来这会起作用,但它变得复杂,因为我不能再使用简单的意图服务(我假设),因为如果意图服务在 onHandleIntent() 之后自行停止,那么将不会收到位置更改事件。

无论如何,它变得有点复杂,所以我想知道是否有更简单的方法。

I have an android application that can operate with or without a location. handling this case in an activity is straightforward: I set a location listener and wait with a progress dialog, but allow the wait to be aborted by the user (it so happens that if the location provider is disabled, there's no special error is just never returns the location ... nice).

However, I also have a service that requires the location. The service may run before the activity,so i can't depend on the activity to get the location (or not).

the question is how to handle this case in the service. i can wait for the location, but since i can't rely on a user abort, how do i know when to stop waiting, and assume that no location is available? it seems i could do something like,

  1. schedule service with action "get location", which sets the location listener, waits for updates, schedules the same service say 3 minutes later with action "refresh"
  2. in service action "refresh", get the last known location, and be happy with that. it was either obtained or not. if it's null, we know we have no location and we've done the best we can.

It seems like this will work, but it's getting complicated as i can no longer use the simple intent service (i assume), because if the intent service stops itself after onHandleIntent(), then the on location changed event won't be received.

Anyway, it's getting sort of complicated, so I'm wondering if there's a simpler way.

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

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

发布评论

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

评论(1

冷…雨湿花 2024-10-07 04:36:57

UpdateService - 实际执行更新工作的意图服务。这个只是调用 getLastKnownLocation() 来获取位置。

LocationService - 注册位置更新的常规服务,并设置一个在 2 分钟内唤醒的计时器。要么我们获得位置更新,要么计时器关闭。如果我们得到位置更新,我们就取消计时器。在这两种情况下,我们都会取消注册位置更新,并启动 UpdateService 来完成实际工作,然后自行停止。

我们将 LocationService 安排为重复警报,作为我们的“后台”进程。

该活动以不同的方式处理事情。它请求位置更新,并弹出一个对话框。我们要么获取位置更新,要么用户取消对话框。不管怎样,我们取消注册位置更新,然后直接启动UpdateService(不是LocationService,我们已经在这里获得了位置)。

UpdateService - intent service that actually does the update work. This one just calls getLastKnownLocation() to obtain the location.

LocationService - regular service that registers for location updates, and also sets a timer to wake up in 2 minutes. either we get the location update, or the timer goes off. if we get the location update, we cancel the timer. in both cases we unregister for location updates, and start UpdateService to do the actual work, then stop ourself.

We schedule LocationService as the repeating alarm to be our "background" process.

The activity handles things differently. It requests location updates, and puts up a dialog. We either get the location update, or the user cancels the dialog. Either way, we unregister for location updates then start UpdateService directly (not LocationService, we already got the location here).

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