我正在实现一种跟踪应用程序,我需要定位一次我的位置并每 10 - 20 秒发送一次(周期值很重要,不能超过)。 为了降低电池消耗,我停止位置更新。这在前台效果很好,但是当应用程序移到后台时我该怎么办? 我查看了有关后台获取的信息,但没有得到定期发送数据的精确时间
我如何执行此任务?
最佳答案
您可以在应用程序处于后台时启动和停止定期位置更新。 要从 Location Update 的给定链接实现此添加类.
之后,在您的 AppDelegate 中导入 LocationTracker.h。
在您的 didFinishLaunchingWithOptions 中添加以下代码。
let locationTracker : LocationTracker = LocationTracker();
locationTracker?.startLocationTracking();
在LocationTracker.m中,可以设置重启更新的时长,这里我设置的是1分钟或者60秒。
//Restart the locationMaanger after 1 minute
self.shareModel.timer = [NSTimer scheduledTimerWithTimeInterval:60 target:self
selector:@selector(restartLocationUpdates)
userInfo:nil
repeats:NO];
您还可以设置获取位置的持续时间。在这里,我获取了 10 秒的位置。
self.shareModel.delay10Seconds = [NSTimer scheduledTimerWithTimeInterval:10 target:self
selector:@selector(stopLocationDelayBy10Seconds)
userInfo:nil
repeats:NO];
关于ios - 在后台模式下定期向服务器发送小数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34512007/