定期iOS背景位置更新 [英] Periodic iOS background location updates

查看:110
本文介绍了定期iOS背景位置更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个需要使用高精度和低频率进行背景位置更新的应用程序。该解决方案似乎是一个后台NSTimer任务,它启动位置管理器的更新,然后立即关闭。之前已经提出过这个问题:

I'm writing an application that requires background location updates with high accuracy and low frequency. The solution seems to be a background NSTimer task that starts the location manager's updates, which then immediately shuts down. This question has been asked before:

如何在我的iOS应用程序中每n分钟更新一次后台位置?

获取应用程序转到后台后每n分钟用户位置

iOS不是典型的后台位置跟踪计时器问题

iOS长时间运行的后台计时器,带有location和location。后台模式

基于位置跟踪的iOS全天候后台服务

但我尚未得到最低示例工作。在尝试了上述接受的答案的每一个排列后,我总结了一个起点。输入背景:

but I have yet to get a minimum example working. After trying every permutation of the above accepted answers, I put together a starting point. Entering background:

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    self.bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        NSLog(@"ending background task");
        [[UIApplication sharedApplication] endBackgroundTask:self.bgTask];
        self.bgTask = UIBackgroundTaskInvalid;
    }];


    self.timer = [NSTimer scheduledTimerWithTimeInterval:60
                                                  target:self.locationManager
                                                selector:@selector(startUpdatingLocation)
                                                userInfo:nil
                                                 repeats:YES];
}

和委托方法:

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
           fromLocation:(CLLocation *)oldLocation {

    NSLog(@"%@", newLocation);

    NSLog(@"background time: %f", [UIApplication sharedApplication].backgroundTimeRemaining);
    [self.locationManager stopUpdatingLocation];

}

目前的行为是 backgroundTimeRemaining 从180秒递减到零(记录位置时),然后执行到期处理程序,不再生成其他位置更新。如何修改上述代码,以便无限期地在后台接收定期位置更新

The current behavior is that the backgroundTimeRemaining decrements from 180 seconds to zero (while logging location), and then the expiration handler executes and no further location updates are generated. How do I modify the above code in order to receive periodic location updates in the background indefinitely?

更新:我的目标是iOS 7,似乎有一些证据表明后台任务的行为有所不同:

Update: I'm targeting iOS 7 and there appears to be some evidence that background tasks behave differently:

从后台任务中启动iOS 7中的位置管理器

推荐答案

似乎 stopUpdatingLocation 是触发后台监视程序计时器的原因,所以我在 didUpdateLocation with:

It seems that stopUpdatingLocation is what triggers the background watchdog timer, so I replaced it in didUpdateLocation with:

[self.locationManager setDesiredAccuracy:kCLLocationAccuracyThreeKilometers];
[self.locationManager setDistanceFilter:99999];

这似乎可以有效地关闭GPS。然后,背景 NSTimer 的选择器变为:

which appears to effectively power down the GPS. The selector for the background NSTimer then becomes:

- (void) changeAccuracy {
    [self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    [self.locationManager setDistanceFilter:kCLDistanceFilterNone];
}

我正在做的是定期切换准确度以获得高精度每隔几分钟协调一次,因为 locationManager 尚未停止, backgroundTimeRemaining 保持其最大值。这样可以减少电池消耗,从每小时约10%(背景中恒定 kCLLocationAccuracyBest )到我的设备每小时约2%。

All I'm doing is periodically toggling the accuracy to get a high-accuracy coordinate every few minutes and because the locationManager hasn't been stopped, backgroundTimeRemaining stays at its maximum value. This reduced battery consumption from ~10% per hour (with constant kCLLocationAccuracyBest in the background) to ~2% per hour on my device.

这篇关于定期iOS背景位置更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆