我想每隔X分钟在后台获得我的位置,我使用的是一个在NSLog上运行良好的线程,它打印一个字符串。然而,当我调用我的locationManager时,它似乎不起作用。
这是我的代码:
- (void)threadEntryPoint:(ActualizarPosicionGPS2AppDelegate *)paramSender{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
while([[NSThread currentThread] isCancelled] == NO){
[NSThread sleepForTimeInterval:10.0f];
if ([[NSThread currentThread] isCancelled] == NO &&
[[UIApplication sharedApplication] backgroundTimeRemaining] != DBL_MAX) {
[self getLocation];
}
}
[pool release];
}在应用程序中:didFinishLaunchingWithOptions:
[NSThread detachNewThreadSelector:@selector(threadEntryPoint:) toTarget:self withObject:self];然后,我得到了以下内容:
-(void) endTaskWidthIdentifier:(NSNumber *)paramIdentifier{
UIBackgroundTaskIdentifier identifier = [paramIdentifier integerValue];
[[UIApplication sharedApplication] endBackgroundTask:identifier];
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
self.backgroundTaskIdentifier = [application beginBackgroundTaskWithExpirationHandler:^{
NSNumber *backgroundTask = [NSNumber numberWithInteger:self.backgroundTaskIdentifier];
[self performSelectorOnMainThread:@selector(endTaskWidthIdentifier:) withObject:backgroundTask waitUntilDone:YES];
self.backgroundTaskIdentifier = UIBackgroundTaskInvalid;
}];
}重要的是,getLocation是这样的:
-(void)getLocation{
if(nil==locationManager)
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDistanceFilter:kCLLocationAccuracyHundredMeters];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager startUpdatingLocation];
NSLog(@"ouch");
if(locationManager.location!=nil){
NSLog(@"obtenido %f, %f",locationManager.location.coordinate.latitude,locationManager.location.coordinate.longitude);
}
}locationManager.location总是返回nil,所以它不会得到当前的位置。
我做错什么了吗?也许我有一个错误的概念!
非常感谢!
发布于 2011-08-01 18:47:54
在这篇文章中可以看到我的答案。
How do I get a background location update every n minutes in my iOS application?
您可能还想检查为什么没有调用您的委托方法
如果您对它感到满意,请将您的项目发送到xs2bush@yahoo.com。我会执行代码,看看哪里出了问题。
https://stackoverflow.com/questions/6617988
复制相似问题