我有一个应用程序,每隔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");
}
答案 0 :(得分:1)
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
[self.locationManager setDistanceFilter:25];
将这些属性更改为不太准确。您可以节省大量电池。 这些配置取决于它是什么类型的应用程序。如果它是某种卡路里燃烧应用程序,它可能是非常准确的。您每2分钟更新一次,因此应将其设置为具有某个阈值的时间覆盖的近似距离的平均值。
答案 1 :(得分:1)
self.locationManager.pausesLocationUpdatesAutomatically=YES;
//monitor locations for significant location change
[self.locationManager startMonitoringSignificantLocationChanges];
如果您的应用确实不需要用户准确定位,您可以尝试使用更多内存优化方式进行重大位置更改,但最终取决于具体情况更重要,即高位置精度或电池用法。我已经对这种方法进行了重大测试,通常每4-5分钟或500米更新一次,以最早者为准。