几分钟不活动后服务停止

发布于 2024-11-02 21:59:44 字数 413 浏览 0 评论 0原文

我正在编写一个 Android 应用程序,在创建主 Activity 时启动的服务中使用 GPS 位置提供程序(除非该服务已在运行)。我的印象是,该服务将继续运行,除非出现不太可能的例外,即如果内存不足,任务管理器可能会终止它。由于某种原因,如果有一段时间活动非常低,无论有多少可用内存,服务都会重新启动。当用户在室外并且 GPS 能够解析位置时,该服务将每 30 秒获取一次位置,并在中间关闭 GPS(以节省电池)。在这种情况下,服务会继续运行,没有任何问题,但当用户移动到室内并且在五秒内找不到 GPS 位置时,服务会关闭 GPS,并且只会每 200 秒重新激活 GPS 以检查是否有 GPS 位置。用户已搬回户外。在这种情况下,服务有时会停止,并且不会重新启动,直到重新打开应用程序并且活动重新启动服务为止。我创建了一个单独的服务来确保该过程不间断。有谁知道为什么会发生这种情况?非常感谢您的帮助! -多姆

I am writing an android app using the GPS location provider in a Service that is started when the main Activity is created (unless the service is already running). I was under the impression that the service would continue to run with the unlikely exception that the task manager could killed it if there was a memory shortage. For some reason the service is restarted if there is a period of very low activity regardless of how much memory is available. When the user is outside and the GPS is able to resolve a position, the service will get a position every 30 seconds and turn off the gps in between (to save battery). In this case the service continues to run without any problems but when a user moves inside and a gps position can't be found within five seconds, then the service turns off the gps and will only reactivate the GPS every 200 seconds to check if the user has moved back outdoors. In this case the service will sometimes be stopped and won't be restarted until the app is reopened and the activity restarts the service. I created a separate service to make sure the process was uninterrupted. Does anyone know why this might be happening? Thanks so much for your help! -Dom

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

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

发布评论

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

评论(1

雪落纷纷 2024-11-09 21:59:44

我想看看 Mark Murphy 处理服务和唤醒锁的示例:

WakefulIntentService

如果可以的话'如果您无法从他的代码示例中弄清楚这一点,那么您需要获取他的《Advanced Android》一书,其中详细解释了该问题。

忙碌的程序员高级 Android 开发指南

这些是有关 Android 开发的一些最好的书籍,非常值得花最少的钱。

I would have a look at Mark Murphy's example that deals with Services and Wakelocks:

WakefulIntentService

If you can't figure it out from his code example, then you want to get his Advanced Android book where the problem is explained in detail.

The Busy Coder's Guide to Advanced Android Development

These are some of the best books on Android development and well worth the minimal cost.

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