[NSTimer scheduledTimerWithTimeInterval:900 target:self selector:@selector(CallGetCounts) userInfo:nil repeats:YES];
这意味着我想每5分钟重复一次计时器,但我的计时器没有重复,无法找到原因
任何人都可以告诉我答案。
我在“AppDelegate
”中写了这个 - > “- (void)applicationDidEnterBackground:(UIApplication *)application
”方法。
答案 0 :(得分:2)
在后台,您的代码将被暂停,并且在您的应用程序再次到达前台之前,您将不会收到任何计时器事件。
您应该按照预定的本地通知,在给定的时间间隔后从后台唤醒。但是,他们会向用户显示他必须首先接受的弹出窗口或横幅。
以下是如何执行此操作的一些步骤:
// When you want to schedule:
UILocalNotification* localNotification = [[[UILocalNotification alloc] init] autorelease];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5]; // seconds
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.alertBody = @"Body text";
localNotification.alertAction = @"Button text";
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
// when it's fired it will call your AppDelegate's didReceiveLocalNotification
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)localNotification
{
// you can handle timer event here and eventually re-schedule your local notification
}
答案 1 :(得分:1)
通常,当应用程序进入后台时,它会被暂停,因此根本不会执行。特别是,NSTimers不会开火。如果您希望在后台执行某些操作,则需要将应用程序配置为在后台运行,并使用一种已批准的方法来执行您要执行的任务。运行NSTimers不是受支持的任务之一。
我建议你查看iOS编程指南,特别是Background Execution and Multitasking部分。
答案 2 :(得分:1)
UILocalNotification
的实例会根据您设置的时间触发弹出框(并唤醒您的应用),如果您真的选择了UILocalNotification
,那么Here是SO线程中讨论的好教程链接。希望那些能帮到你。