简体   繁体   English

在多任务iPhone应用程序的后台运行NSTimer以打开/关闭定位服务

[英]running an NSTimer in the background of a multitasking iPhone app to turn location service on/off

i am attempting to make an app that records an accurate location every 10 minutes. 我正在尝试制作一个每10分钟记录一次准确位置的应用程序。

the problem with this is that when running is best mode, the battery drains really quickly. 问题在于,当以最佳模式运行时,电池会很快耗尽。

i would like a timer to turn of the location service, have didUpdateToLocation get a good location, turn of location service and then wait for the timer again. 我想要一个打开定位服务的计时器,didUpdateToLocation获取了一个好的位置,打开了定位服务,然后再次等待计时器。

i have tried a million things- but my NSTimer always gets frozen when put in background mode. 我已经尝试了上百万种东西,但是当我进入后台模式时,我的NSTimer总是会冻结。

interestingly, didUpdateFromLocation can turn location service off, and if you use significantChangeUpdate you can use didUpdateToLocation to turn location service back on when the user passes a triangulation boundry- and record locations to coredata- 有趣的是,didUpdateFromLocation可以关闭位置服务,如果您使用了ificantChangeUpdate,则可以在用户通过三角剖分边界并将位置记录到coredata时使用didUpdateToLocation重新打开位置服务。

so in theory the only thing stopping me getting this thing running is an NSTimer in a thread that doesnt get frozen, ever. 因此,从理论上讲,阻止我运行该程序的唯一方法是一个永远不会冻结的线程中的NSTimer。

i read some documents that said you CAN do simple things in the background beyond VOIP, location, audio. 我读过一些文件,说您可以在VOIP,位置和音频之外的背景中做一些简单的事情。 does anyone have any experience getting a thread running that doesnt get frozen? 有没有人有任何使运行的线程不会冻结的经验? i know everyone thinks it can't be done- from my research it is possible, but i just cant figure it out. 我知道每个人都认为这是不可能的-从我的研究中有可能,但我只是想不通。

advice based on any experience getting a thread running in the background beyond VOIP, Audio, location would be much appreciated 根据任何使线程在VOIP,音频,位置以外的背景中运行的经验的建议,将不胜感激

thanks guys, dave- 谢谢大家,戴夫

The answer to this question has an alternative hack where you use re-use the applicationDidEnterBackground event to loop back on itself, each time re-starting a thread where you can do some work 这个问题的答案有一个替代性的解决方法,您可以在其中重新使用applicationDidEnterBackground事件来循环返回自身,每次重新启动线程时您都可以进行一些工作

iPhone - Backgrounding to poll for events iPhone-轮询事件背景

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM