我正在为iPhone编写基于位置的应用程序。该应用程序是一个后台运行的应用程序,所以我注册它以进行后台更新。 当应用程序进入后台我打电话:
- (void) enteredBackground: (NSNotification*) notification {
[self.locationManager stopUpdatingLocation];
NSLog(@"LocationManager => stopUpdatingLocation called");
[self.locationManager startMonitoringSignificantLocationChanges];
NSLog(@"LocationManager => startMonitoringSignificantLocationChanges called");
}
在AppDelegate中我打电话:
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSLog(@"applicationDidEnterBackground");
[[NSNotificationCenter defaultCenter] postNotificationName: @"didEnterBackground" object: nil userInfo: nil];
UIApplication *app = [UIApplication sharedApplication];
if ([app respondsToSelector:@selector(beginBackgroundTaskWithExpirationHandler:)]) {
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
dispatch_async(dispatch_get_main_queue(), ^{
if (bgTask != UIBackgroundTaskInvalid)
{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}
});
}];
}
}
当应用程序被FastAppSwitcher杀死时,会触发通知并调用以下方法:
-(void) exitApplicationFromBackground:(NSNotification *)notification{
NSLog(@"exitApplicationFromBackground called");
[self.locationManager stopMonitoringSignificantLocationChanges];
NSLog(@"LocationManager => stopMonitoringSignificantLocationChanges called");
NSLog(@"LocationManager stopped, Application will terminate...");
exit(0);
}
我测试了很多次。每次将所有NSLog写入控制台或在iPhone上测试日志文件时。但不是每次灰色位置箭头从状态栏消失,尤其是在iPhone上运行应用程序时。
怎么可能?谢谢你的回答!!