2011-11-21 74 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:^{ 
      }]; 

装上去的到期处理,这将使具有另一个到期处理,使具有其他处理程序,使另一个计时器另一个定时器...