[英]iOS Background app : best way to run app
我需要創建一個在后台模式下檢索iOS廣播信息(rssi,bearer等)的應用程序; 該應用程序將在應用程序商店中提供,並因此符合應用程序商店指南。 該應用程序應合理保存手機的電池。
我目前將BackgroundFetch和GPS一起使用,效果很好,但是當iOS內存不足時,該應用將被OS殺死。
我還看過使用GPS並保持iPhone電池壽命的Google應用(Google Now),但我不知道他們是怎么做到的。
謝謝
“ [startUpdatingLocation]通常要求在更長的時間內啟用位置跟蹤硬件,這可能導致更高的功耗。” “ [with startMonitoringSignificantLocationChanges]應用程序可以在設備距其先前的通知移動500米或更遠時立即收到通知。它不應每隔五分鍾收到一次通知。
您可能會看到,使用startMonitoringSignificantLocationChanges可以大大延長電池壽命。 這是我在AppDelegate.m中實現它的方式:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
...
[self.locationManager startMonitoringSignificantLocationChanges];
...
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
...
[self.locationManager stopUpdatingLocation];
[self.locationManager startMonitoringSignificantLocationChanges];
...
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
...
[self.locationManager stopMonitoringSignificantLocationChanges];
[self.locationManager startUpdatingLocation];
...
}
-(CLLocationManager *)locationManager{
if(!_locationManager){
_locationManager = [[CLLocationManager alloc]init];
_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
_locationManager.distanceFilter = 5;
}
return _locationManager;
}
這樣,我可以確保僅在應用程序處於活動狀態時才使用耗電的“ startUpdatingLocation”方法,而在不活動狀態時僅使用省電的“ stopMonitoringSignificantLocationChanges”。
我對上面的代碼進行了測試。
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
//NSLog(@"Location: %f, %f", newLocation.coordinates.longtitude, newLocation.coordinates.latitude);
longitude = newLocation.coordinate.longitude;
latitude = newLocation.coordinate.latitude;
gpsTimestamp = [newLocation.timestamp timeIntervalSince1970];
NSLog(@"Changed ! ");
if (!timer && ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground)) {
NSLog(@"Starting task");
timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(createBgIndicator) userInfo:nil repeats:YES];
}
}
在這種情況下,計時器將僅在我移動時啟動,並執行5次(10秒)。 我想讓計時器一直運行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.