xcode 计时器:如何在后台保持计时器处于活动状态
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8613487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Timer: how to remain timer active in Background
提问by Arpit B Parekh
In my iPhone Timer Application,
在我的 iPhone 定时器应用程序中,
In which a timer should run in background.
其中计时器应在后台运行。
So, I have set the notification in appdelegate it works perfectly... With that I am calling the methods from view controller which makes timer alive.
所以,我已经在 appdelegate 中设置了通知,它工作得很好......我正在调用视图控制器中的方法,这使得计时器处于活动状态。
Take a look some code...
看一段代码...
App delegate
应用委托
- (void)applicationDidEnterBackground:(UIApplication *)application
{
/*
Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
*/
NSLog(@"Time Remaining %d",(self.viewController.totalSeconds-self.viewController.totalCount));
[self.viewController selectandnotify:(self.viewController.totalSeconds-self.viewController.totalCount)];
[self.viewController stopTimer];
[self.viewController startTimerAction];
}
Here I am calling the method startTimerActionmethod which is in my view controller...take a look at this...
在这里,我正在调用视图控制器中的startTimerAction方法……看看这个……
-(void)startTimerAction
{
timer_main = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(ShowActicity) userInfo:nil repeats:YES];
}
Which is NSTimer
哪个是 NSTimer
Here every time
每次都在这里
-ShowActivitymethod will call after each second...Which is below in my view controller...
- ShowActivity方法将在每秒后调用...在我的视图控制器中如下所示...
-(void)ShowActicity
{
NSLog(@"Total Counts %d",totalCount);
if (totalCount == totalSeconds) {
if ([timer_main isValid]) {
[timer_main invalidate];
isTimeOver = YES;
[self generateLog];
}
} else {
totalCount++;
seconds =seconds + 1;
if(seconds > 59)
{
minutes = minutes + 1;
seconds= 0;
}
}
}
How to call each time This method from view controller.....
每次如何从视图控制器调用此方法.....
How can I call each time showActivity method from appdelegate...
Should I use delegate for that
Should I create showActivity and timer in my Appdelegate..
Actually I want this application to run when view switches in app.....
我如何每次从 appdelegate 调用 showActivity 方法...
我应该为此使用委托吗
我应该在我的 Appdelegate 中创建 showActivity 和计时器吗?
其实我希望这个应用程序在应用程序中的视图切换时运行.....
I think If I make delegate is a good option?
我认为如果我委托是一个不错的选择?
Any other way....please have some suggestions
任何其他方式....请有一些建议
回答by Tendulkar
Generally use this code for background running .In the Background timer doesn't work
一般使用此代码进行后台运行。在后台计时器不起作用
- (void)applicationDidEnterBackground:(UIApplication *)application
{
UIApplication* app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Do the work associated with the task.
[self startTimerAction];
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
});
}