定期 iOS 后台位置更新 [英] Periodic iOS background location updates

查看:43
本文介绍了定期 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 长时间运行的带有位置"的后台计时器;后台模式

基于位置跟踪的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 替换了:

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天全站免登陆