定时器:如何在后台保持定时器活动 [英] Timer: how to remain timer active in Background

查看:202
本文介绍了定时器:如何在后台保持定时器活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



在后台运行计时器


我已经在appdelegate中设置通知它工作完美...
我从调用方法从视图控制器,使定时器生效。



看一些代码...



应用委托

  - (void)applicationDidEnterBackground:(UIApplication *)application 
{
/ *
使用此方法释放共享资源,保存用户数据,使计时器无效并存储足够的应用程序状态信息,应用程序到其当前状态,以防它以后终止。
如果应用程序支持后台执行,则调用此方法,而不是applicationWillTerminate:当用户退出时。
* /

NSLog(@剩余时间%d,(self.viewController.totalSeconds-self.viewController.totalCount));
[self.viewController selectandnotify:(self.viewController.totalSeconds-self.viewController.totalCount)];
[self.viewController stopTimer];
[self.viewController startTimerAction];

}

这里我调用方法 startTimerAction 方法,在我的视图控制器...看看这个...

   - (void)startTimerAction 
{
timer_main = [NSTimer scheduledTimerWithTimeInterval:(1.0)target:self selector:@selector(ShowActicity)userInfo:nil repeats:YES];
}

这是NSTimer



这里每次



- 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;
}

}



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




  • 如何调用每个showActivity方法从appdelegate ...




    • 我应该使用委托


    • <我应该在我的Appdelegate中创建showActivity和计时器。



  • 当视图在应用程序中切换.....




我认为如果我使委托是一个好的选择? p>

任何其他方式....请提出一些建议

解决方案

使用此代码进行后台运行。在后台定时器不工作

   - (void)applicationDidEnterBackground:(UIApplication *)application 
{
UIApplication * app = [UIApplication sharedApplication];

bgTask = [app beginBackgroundTaskWithExpirationHandler:^ {
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];

//启动长时间运行的任务并立即返回。
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),^ {

//与任务相关的工作
[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


In my iPhone Timer Application,

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.

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 startTimerAction method which is in my view controller...take a look at this...

-(void)startTimerAction
{
 timer_main = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self    selector:@selector(ShowActicity) userInfo:nil repeats:YES];
}

Which is NSTimer

Here every time

-ShowActivity method will call after each second...Which is below in my view controller...

-(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.....

I think If I make delegate is a good option?

Any other way....please have some suggestions

解决方案

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;
    });
}

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

这篇关于定时器:如何在后台保持定时器活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆