停止 CoreLocation 后台更新
我的应用程序注册后台位置更新(不是重大变化。我需要比公里更多的粒度)
该部分工作正常。我只想在应用程序在后台运行 60 分钟后停止更新。我现在执行此操作的方法是在 CLLocationManager 委托中调用 stopUpdatingLocation
。
但是,我不确定这是否会真正阻止我的应用程序接收后台位置更新,或者是否会,但只会忽略它们。理想情况下,除了用户活动后的一小段时间外,我不希望我的应用程序对电池消耗负责。
苹果的文档往往让我在这个问题上陷入困境。就在我认为“在后台执行代码”即将泄露答案时,它引导我阅读“位置感知编程指南”。当那个人要具体说明这个问题时,它让我回到“在后台执行代码”。 :)
My app registers for background location updates (not Significant Change. I need more granularity than kilometers)
That part works fine. I just want to stop the updates after the app has been in the background for 60 minutes. The way I'm doing it right now is to call stopUpdatingLocation
in my CLLocationManager delegate.
However, I am unsure if this will actually stop my app from receiving background location updates, or if it will, but will just ignore them. Ideally, I'd like to not have my app be responsible for battery drain except for a small period of time after user activity.
Apple's documentation tends to send me in circles on this one. Right when I think "Executing Code in the Background" is about to spill the answer, it directs me to "Location Awareness Programming Guide." Right when that one is about to get specific about this question, it sends me back to "Executing Code in the Background." :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定您的应用是否对此具有完全控制权,但
stopUpdatingLocation
的文档确实声明了以下内容...这表明您的应用程序是否是唯一请求后台位置数据的应用程序,并且您请求它停止 GPS 接收器会禁用适当的硬件以节省电量,我认为这正是您正在寻找的。
我可以建议您在设置 -> 中启用电源记录。开发人员,然后运行您的应用一个小时左右,直到您认为 GPS 已禁用,再运行一个小时,然后比较 Instruments 中的电量使用情况。这将为您提供更清晰的画面。
I'm not sure whether your app has total control over this, but the docs for
stopUpdatingLocation
do state the following ...Which would suggest that should your app be the only one requesting background location data, and you request it to stop the GPS receiver would disable the appropriate hardware to save on power, which I think it was you're looking for.
What I can suggest is that you enable Power logging in Settings -> Developer, then run your app for an hour or so to a point where you think GPS is disabled, run for another hour and then compare the power usage in Instruments. This should provide you with a much clearer picture.
调用
stopUpdatingLocation
确实会停止 GPS 等,但如果您将此调用放在CLLocationManager delegate
的locationManager:didUpdateToLocation:fromLocation
内,那么它可能永远不会被调用。请记住,只有当更准确的位置或(根据活动距离过滤器)显着不同的位置可用时,才会执行此方法。如果设备根本没有移动,并且影响精度的情况也没有改变,则可能几个小时都不会被调用(我自己验证了几分钟),并且 GPS 也会保持启用状态这么长时间。
对于仅在前台运行的应用程序,您可以使用 60 分钟的
NSTimer
,但对于在后台运行的应用程序,迄今为止我发现的最接近可行的解决方案是计划 <代码>UILocalNotification。但即便如此,也需要用户在应用程序重新启动并关闭 GPS 之前确认警报。也许
beginBackgroundTaskWithExpirationHandler:
是更好解决方案的关键,但我还没有完全弄清楚。Calling
stopUpdatingLocation
will indeed stop GPS and so on, but if you place this call insidelocationManager:didUpdateToLocation:fromLocation
of yourCLLocationManager delegate
, then it might never be called.Keep in mind that this method is only executed whenever a more accurate position or a (according to the active distance filter) significantly different position is available. If the device does not move at all and if the circumstances affecting the accuracy also do not change, it may not be called for hours (I have verified it for several minutes myself) and GPS will also stay enabled for this long.
For apps that only work in the foreground, you can use an
NSTimer
of 60minutes, but for apps that run in the background, the closest to a working solution that I have found so far, is a scheduledUILocalNotification
. But even that requires the user to confirm an alert, before the app is restarted and able to turn the GPS off.Maybe
beginBackgroundTaskWithExpirationHandler:
is the key to a better solution, but I have not completely figured it out, yet.