【发布时间】: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