在后台运行NSTimer或UILocalNotification

时间:2011-11-21 18:23:51

标签: ios background nstimer uilocalnotification

如何在iOS上执行倒计时器?

首先,我尝试NSTimer。然后,当应用程序进入后台时,我发现NSTimer无效。

其次,我用了

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

NSTimer在后​​台工作。但后来我发现iOS只允许应用程序在后台工作不超过600秒(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时间点击警报按钮时,如何调用我自己的方法。

非常感谢!

1 个答案:

答案 0 :(得分:0)

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

在expiration处理程序中添加一些东西,它将生成另一个具有另一个到期处理程序的计时器,该处理程序使另一个计时器具有另一个处理程序...