在后台模式下或在应用程序终止后使用didFinishLaunchingWithOptions处理位置事件

时间:2012-06-13 09:57:40

标签: background cllocationmanager cllocation

我在位置管理器init中调用startMonitoringSignificantLocationChanges。

我希望如果有位置事件,将启动didFinishLaunchingWithOptions并执行以下代码。 (包括后台模式和应用程序终止)

应用程序冻结(当我尝试在不同位置的后台播放几小时后启动黑屏)

当我得到一个位置事件时,我可能做错了什么。如果有人知道问题是什么,我将不胜感激。

顺便说一句,没有办法模拟这种位置事件,但物理上移动到不同的单元塔的不同位置。在那儿? ...... :(

LocationController.m:

- (id)init
{
    self = [super init];
    if (self != nil) {
        self.locationManager = [[[CLLocationManager alloc] init] autorelease];
        self.locationManager.delegate = self;
        self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
        [self.locationManager startUpdatingLocation];
        [self.locationManager startMonitoringSignificantLocationChanges];
        [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(stop) userInfo:nil repeats:NO];
    }
    return self;
}
appdelegate.m : 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    id locationValue = [launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey];
    if (locationValue)
    {
        [[LocationController sharedInstance] startMonitoringSignificantLocationChanges];
        UIApplication *app  = [UIApplication sharedApplication];
        bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ 
            [app endBackgroundTask:bgTask]; 
            bgTask = UIBackgroundTaskInvalid;
            }];
        [self updateLocationService];
        return YES;
    }
}
- (void) updateLocationService {
    [[LocationController sharedInstance] start];
    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(stop) userInfo:nil repeats:NO];    
}
- (void) stop {
    [[LocationController sharedInstance] stop];
}

谢谢!

1 个答案:

答案 0 :(得分:2)

如果没有设置UIApplicationLaunchOptionsLocationKey,你的didFinishLaunchingWithOptions:方法不会返回任何内容。

只需将return语句移到if子句之外:

if (locationValue)
{ 
    ... 
}
return YES;