2012-01-16 29 views
2

我有一個計時器,顯示用戶工作的NSTime。當我的應用進入後臺模式時,位置管理器和該計時器停止更新。當我的應用程序處於後臺模式時,如何讓它們更新?當我的應用程序進入後臺模式時,如何更新位置管理器和計時器?

我有一個叫做RunViewController的視圖,它有啓動按鈕。當用戶點擊該按鈕時,定時器和位置管理器啓動。代碼是:

-(void)startRun{ 
    timeSec = 0; 
    timeMin = 0; 
    timeHour = 0; 

    NSString* timeNow = [NSString stringWithFormat:@"%02d:%02d:%02d",timeHour , timeMin, timeSec]; 
    //Display on your label 
    lblTime.text = timeNow; 

    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerTick:) userInfo:nil repeats:YES]; 
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode]; 

    self.locManager = [[CLLocationManager alloc] init]; 
    locManager.delegate = self; 
    locManager.desiredAccuracy = kCLLocationAccuracyBest; //kCLLocationAccuracyBest 
    [locManager setDistanceFilter:3]; //kCLDistanceFilterNone 

    [locManager startUpdatingLocation]; 
    locationManagerStartDate = [[NSDate date] retain]; 


} 

在 - (空)的LocationManager:(CLLocationManager *)經理didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

簡單地畫一條線在地圖上的老位置新位置並將該位置保存在數組中。

在此先感謝。

+0

發佈您的一些代碼。還發布位置更新管理器代碼 – Hiren 2012-01-16 08:35:18

回答

0

試試這個..

-(void)viewDidLoad 
{ 
    UIDevice* device = [UIDevice currentDevice]; 
    BOOL backgroundSupported = NO; 
    if ([device respondsToSelector:@selector(isMultitaskingSupported)]) 
     backgroundSupported = device.multitaskingSupported; 

    NSLog(backgroundSupported ? @"Yes" : @"No"); 


    counterTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ 
    //called after 10min of your app in background 
     NSLog(@"10 minutes finished."); 
    }]; 

    theTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(selectorForUpdatingLocation) userInfo:nil repeats:YES]; 

} 
1

找到了解決方案與蘋果開發者論壇的幫助下實現這一點。我做了以下內容:

  • 指定位置的背景模式

  • 使用

  • 的UIApplication使用一個NSTimer背景:beginBackgroundTaskWithExpirationHandler:如果n是

小比UIApplication:backgroundTimeRemaining它確實可行 罰款,如果n較大,則外景經理應再次啓用 (和殘疾人)之前還有剩餘,以避免 後臺任務被殺死沒有時間。由於位置是 三種允許的後臺執行類型之一,所以這起作用。

注:沒有通過在模擬器,它不工作,測試這種寬鬆一些時間,工作正常,我的手機上。

Here是相同的參考。

相關問題