2011-11-21 70 views
4

如何在iOS上執行倒數計時器?在後臺運行NSTimer或UILocalNotification

首先,我試了NSTimer。然後我發現NSTimer在應用進入後臺時無法工作。

其次,我用

counterTask = [[UIApplication sharedApplication] 
       beg 
inBackgroundTaskWithExpirationHandler:^{ 
       }]; 

,讓在後臺NSTimer工作。但後來我發現iOS只允許應用程序在不超過600s(10分鐘)的背景下工作。我不知道如何讓NSTimer在後臺超過10分鐘。

三,我厭倦了使用NSLocalNotification,而且效果很好。但我想要做的是通過調用一種方法來暫停播放音樂。我發現UILocalNotification將在時間顯示警報。如果您沒有點擊提醒按鈕,則不會調用方法 - (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notification。我不知道如何在不點擊警報按鈕的情況下調用方法。

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notification { 
    NSLog(@"Recieved Notification %@",notification); 
    UIAlertView *alert=[[[UIAlertView alloc] initWithTitle:@"Local Notification" message:@"message" 
           delegate:nil cancelButtonTitle:nil 
        otherButtonTitles:nil, nil] autorelease]; 
    [alert show]; 
} 

所以,問題是 1.如何讓在後臺運行NSTimer超過10分鐘, 或 2.如何叫我自己的方法,無需單擊警報按鈕時,它是時間UILocalNotification

非常感謝!

回答

0
counterTask = [[UIApplication sharedApplication] 
      beginBackgroundTaskWithExpirationHandler:^{ 
      }]; 

裝上去的到期處理,這將使具有另一個到期處理,使具有其他處理程序,使另一個計時器另一個定時器...