如何在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
。
非常感謝!