Android 服务始终开启 GPS
我已经创建了一个 Android 应用程序,到目前为止运行良好。它包含一个本地服务,负责每隔几分钟获取用户位置,并通过 UDP 将其转发到收集服务器,该收集服务器又显示在 Web 应用程序地图上。
除非用户明确退出应用程序,否则应用程序需要持续运行。目前,操作系统似乎正在关闭该服务并根据需要重新启动它。我可以看到这一点,因为通常它会每 2 分钟一致地报告其位置。相反,这些间隔之间存在间隙。
因此,我正在寻找一些关于如何编写一项服务的建议,该服务(不幸的是,以电池为代价)可以始终保持运行,并将位置服务集中起来,足以以 2 分钟的间隔发送准确的修复信息。
到目前为止,我有一个运行良好的代码库,因此,如果通过代码演示我当前的位置会有所帮助,请随时询问。
预先感谢那些伸出援手的人。
I've created an android app that is so far working quite well. It contains a local service which is responsible for getting the users location every couple of minutes and relaying that via UDP to a collection server which is in turned displayed on a web application map.
The app needs to be constantly running unless the user explicitly signs out of the application. Currently, it seems the OS is shutting down the service and restarting it as needed. I can see this because normally it would report its location consistently every 2 minutes. Instead, there are gaps in these intervals.
So I'm looking for some advice on how I can write a service which (at the battery's expense unfortunately) can stay running always and pool the location service just enough to send accurate fixes at a 2 minute interval.
I have a well working code base so far, so if demonstrating where I currently am with code would help, don't hesitate to ask.
Thanks in advance to those that lend a hand.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您最好的选择是使用 AlarmManager 每两分钟唤醒一次,做一些处理并快速回去睡觉。
如果您认为自己是硬核,您甚至可以使用唤醒锁,但这将使手机的 CPU 毫不留情地持续开启。你不想要这样。
Your best bet is to use the AlarmManager to wake up every two minutes, do some processing and quickly go back to sleep.
If you think you're hardcore, you could even use a wake lock, but that will keep the phone's CPU turned on persistently without mercy. You don't want that.