[英]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.