【问题标题】:iOS: Background location app using too much batteryiOS:后台定位应用程序使用过多电池
【发布时间】:2015-09-08 12:46:15
【问题描述】:

我有一个应用程序,它每 2 分钟在后台获取位置更新,每行驶 25 米。我的问题是,虽然位置跟踪运行良好,但它严重耗尽了我的电池。 24 小时内的电池使用量为 30%,而我的下一个最接近的应用程序为 14%。是否有一些技巧可以减少应用的电池使用量,同时保持位置更新的准确性和时间段?

是否会因为调用 didUpdateLocations: 的频率而耗尽电池电量?或者可能是我在每次位置更新时都在后台与 Web 服务进行通信?我真的不知道如何提高电池使用率,非常感谢任何帮助!

以下是我在 AppDelegate 中用于获取位置并向服务器发送更新的代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
     self.locationManager = [CLLocationManager new];
    [self.locationManager setDelegate:self];
    if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
        [self.locationManager requestAlwaysAuthorization];
    }
    [self.locationManager startUpdatingLocation];
    [self.locationManager startMonitoringSignificantLocationChanges];

    [self initializeRegionMonitoring];
}

-(void) initializeRegionMonitoring {
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;

    // notify changes when the device has moved x meters
    self.locationManager.distanceFilter = 25; // or set to 20 meters
    self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
    [self.locationManager startUpdatingLocation];
    [self.locationManager startMonitoringSignificantLocationChanges];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSDate *lastDate = [defaults objectForKey:@"lastSavedDate"];

    if ([[NSDate date] timeIntervalSinceDate:lastDate] < 120) {
        NSLog(@"LAST SAVE TIME LESS THAN 2 MINUTES");
        return;
    }

    [self saveLocation];    

    //tell the centralManager that you want to deferred this updatedLocation
    if (isBackgroundMode && !_deferringUpdates)
    {
        NSLog(@"THIS IS GETTING CALLED EVERY TIME");
        _deferringUpdates = YES;
        [self.locationManager allowDeferredLocationUpdatesUntilTraveled:CLLocationDistanceMax timeout:120];
    }
}

- (void) locationManager:(CLLocationManager *)manager didFinishDeferredUpdatesWithError:(NSError *)error {

    _deferringUpdates = NO;
}

-(void) saveLocation {
    // This code saves location data to a web service
}

- (void)applicationWillResignActive:(UIApplication *)application {
    isBackgroundMode = YES;

    [self.locationManager stopUpdatingLocation];
    [self.locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
    [self.locationManager setDistanceFilter:25];
    self.locationManager.pausesLocationUpdatesAutomatically = NO;
    self.locationManager.activityType = CLActivityTypeAutomotiveNavigation;
    [self.locationManager startUpdatingLocation];
}


-(BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
    return true;
}

-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {

    [self saveLocation];

   completionHandler(UIBackgroundFetchResultNewData);
    NSLog(@"Fetch completed");
}

【问题讨论】:

  • [self.locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters]; [self.locationManager setDistanceFilter:25]; 将属性更改为不太准确。您可以节省大量电池电量。
  • 我已将其更改为 setDistanceFilter:50 和 setDesiredAccuracy:kCLLocationAccuracyHundredMeters。这会显着影响我的位置更新的准确性吗?出于我的目的,我希望位置比 100 米更准确
  • 这取决于它是什么类型的应用程序。如果它是某种卡路里燃烧应用程序,它可能会非常准确。您每两分钟更新一次,因此应将其设置为在一定时间内覆盖的近似距离的平均值。
  • 将更新之间的时间从 2 分钟移到 5 分钟是否会对电池使用有显着的好处?
  • 好的,如果您想发布我们在实际答案中涵盖的所有这些信息,我很乐意接受,谢谢您的帮助!

标签: ios objective-c geolocation cllocationmanager cllocation


【解决方案1】:
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];    
[self.locationManager setDistanceFilter:25];

将这些属性更改为不太准确。您可以节省大量电池电量。 这些配置取决于它是什么类型的应用程序。如果它是某种卡路里燃烧应用程序,它可能会非常准确。您每 2 分钟更新一次,因此应将其设置为具有一定阈值的时间所覆盖的近似距离的平均值。

【讨论】:

    【解决方案2】:
    self.locationManager.pausesLocationUpdatesAutomatically=YES;
    //monitor locations for significant location change
    [self.locationManager startMonitoringSignificantLocationChanges];
    

    如果您的应用不需要用户的准确位置,您可以尝试使用显着的位置更改,这是更优化内存的方式,但最终取决于情况,即高定位精度或电池使用情况更重要。我已经对这种方法进行了重要的测试,通常它每 4-5 分钟或 500 米更新一次位置,以最早者为准。

    【讨论】:

    • 我需要50米左右范围内相对准确的数据。将 pausesLocationUpdatesAutomatically 设置为 YES 有什么作用?
    • 它授权操作系统在用户长时间不移动时停止向应用更新位置。
    • 它如何知道用户在更长的时间内不会移动??
    • 我相信如果你的应用需要持续的位置更新,它可能是一个跟踪运动的导航或健身应用,如果用户在某些情况下根本没有移动,最好让系统决定推迟位置更新。请参阅此了解更多信息 [developer.apple.com/library/ios/documentation/CoreLocation/…
    • 正是我所需要的,设置 pausesLocationUpdatesAutomatically = YES 将允许位置管理器关闭电源,在不牺牲位置数据的情况下节省电池寿命。完美!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多