首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >定时器:如何保持定时器在后台活动

定时器:如何保持定时器在后台活动
EN

Stack Overflow用户
提问于 2011-12-23 07:47:18
回答 1查看 6.6K关注 0票数 3

在我的iPhone计时器应用程序中

其中计时器应该在后台运行。

所以,我已经将通知设置为set委托--它工作得很好.使用该方法,我将从视图控制器调用方法,使计时器活动。

看看一些代码..。

App委托

代码语言:javascript
复制
- (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];

}

在这里,我调用方法startTimerAction方法,在我看来,controller...take是看这个.

代码语言:javascript
复制
-(void)startTimerAction
{
 timer_main = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self    selector:@selector(ShowActicity) userInfo:nil repeats:YES];
}

那就是NSTimer

每次都在这里

-ShowActivity方法将在每个second...Which在我的视图控制器下面之后调用.

代码语言:javascript
复制
-(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;
        }

}

如何每次从视图控制器调用此方法.

如何每次从appdelegate...调用showActivity方法?

代码语言:javascript
复制
- Should I use delegate for that
- Should I create showActivity and timer in my Appdelegate..

  • 实际上我希望这个应用程序在应用程序中视图切换时运行……

我认为如果我做代表是一个好的选择?

任何其他way....please都有一些建议

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-12-23 08:07:06

通常将此代码用于运行.In的后台,后台计时器不工作

代码语言:javascript
复制
- (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;
    });
}

http://developer.apple.com/library/ios/#DOCUMENTATION/iPhone/Conceptual/iPhoneOSProgrammingGuide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html#//apple_ref/doc/uid/TP40007072-CH4-SW3

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8613487

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档