2015-09-08 59 views
1

我有一個應用程序,每隔2分鐘和25米就會在後臺獲取位置更新。我的問題是,當位置跟蹤工作正常時,它正在嚴重耗盡我的電池。 24小時內的電池使用率爲30%,而下一個最接近的應用爲14%。是否有一些技巧來減少我的應用程序的電池使用量,同時保持我的位置更新的準確性和時間段?iOS:使用太多電池的背景位置應用程序

由於調用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

'[self.locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters]; [self.locationManager setDistanceFilter:25];'將屬性更改爲不太準確。您可以節省大量電池。 –

+0

我已將其更改爲setDistanceFilter:50和setDesiredAccuracy:kCLLocationAccuracyHundredMeters。這是否會顯着影響我的位置更新的準確性?爲了我的目的,我寧願位置要比100米更精確 –

+0

以及它取決於它是什麼樣的應用程序。如果它是某種卡路里燒傷應用程序,它可能非常準確。你每兩分鐘更新一次,所以它應該設置在具有某個閾值的時間內的近似距離的平均值。 –

回答

1
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];  
[self.locationManager setDistanceFilter:25]; 

更改這些屬性不太準確。您可以節省大量電池。 這些配置取決於它是什麼類型的應用程序。如果它是某種卡路里燒傷應用程序,它可能非常準確。您每2分鐘更新一次,因此它應該設置爲具有某個閾值的近似距離的平均值。

1
self.locationManager.pausesLocationUpdatesAutomatically=YES; 
//monitor locations for significant location change 
[self.locationManager startMonitoringSignificantLocationChanges]; 

如果你的應用需要簡化版,您可以嘗試使用顯著的位置變化,這是更多的內存優化的方式但最終它取決於什麼是更重要的,即高定位精度或情況下,用戶的精確定位電池使用情況。我已經對這種方法進行了重要的測試,通常每4-5分鐘或500米更新一次,以最早的時間爲準。

+0

我需要相對準確的數據在約50米範圍內。什麼設置暫停LocationLocationUpdatesAutomatically爲YES呢? –

+0

它授權操作系統停止更新位置到應用程序,當用戶不會移動更長的時間。 –

+0

它如何知道用戶不會長時間移動? –