从后台任务运行NSTimer

时间:2013-01-23 11:57:45

标签: iphone ios

我想使用NSTimer作为后台任务。我写了这段代码:

UIBackgroundTaskIdentifier bgTask;
        UIApplication  *app = [UIApplication sharedApplication];
        bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
            [app endBackgroundTask:bgTask];
        }];
        self.silenceTimer = [NSTimer scheduledTimerWithTimeInterval:30 target:self selector:@selector(findlocation:) userInfo:nil repeats:NO];

但它在30秒后不会触发findlocation方法。请指导我如何做到这一点。

4 个答案:

答案 0 :(得分:0)

当应用处于后台状态时,

NSTimer会暂停。

你必须开始一些后台任务才能做你想做的事。但即使如此,在将应用程序置于后台后,您将被限制在一定的时间内。

仅为位置跟踪,VoIP或音频应用授予实际后台行为。其他应用必须面临限制:一旦在后台,您将有足够的时间来完成以beginBackgroundTaskWithExpirationHandler: ( backgroundTimeRemaining )开头的任务。

检查以获取更多信息http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/BackgroundExecution/BackgroundExecution.html

要获得干净的解决方案,请检查此问题的接受答案How do I make my App run an NSTimer in the background?

答案 1 :(得分:0)

当应用程序进入后台时播放声音的代码,您可以自定义它以供自己使用

- (void)applicationDidEnterBackground:(UIApplication *)application{
    backgroundTask = [application beginBackgroundTaskWithExpirationHandler: ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            if (backgroundTask != UIBackgroundTaskInvalid)
            {
                [application endBackgroundTask:backgroundTask];
                backgroundTask = UIBackgroundTaskInvalid;
            }
        });
    }];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        [NSThread sleepForTimeInterval:3];
        [self startPlayingInBackground:@"default.aif"];
        NSLog(@"Time remaining: %f",[application backgroundTimeRemaining]);

        dispatch_async(dispatch_get_main_queue(), ^{
            if (backgroundTask != UIBackgroundTaskInvalid)
            {
                // if you don't call endBackgroundTask, the OS will exit your app.
                [application endBackgroundTask:backgroundTask];
                backgroundTask = UIBackgroundTaskInvalid;
            }
        });
    });
}

下面的代码是帮助我播放声音的代码,这是我的目标c函数

[self startPlayingInBackground:@"default.aif"];

reference from

答案 2 :(得分:0)

UIBackgroundTaskIdentifier bgTask;
    UIApplication  *app = [UIApplication sharedApplication];
    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
        [app endBackgroundTask:bgTask];
    }];
    self.silenceTimer = [NSTimer scheduledTimerWithTimeInterval:30 target:self selector:@selector(findlocation:) userInfo:nil repeats:NO];

[[NSRunloop mainRunLoop] addTimer:self.silenceTimer forMode:NSDEfaultRunLoopMode];

答案 3 :(得分:-1)

试试这段代码:

dispatch_queue_t backgroundQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
 dispatch_source_t timerSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, backgroundQueue);
 dispatch_source_set_timer(timerSource, dispatch_time(DISPATCH_TIME_NOW, 0), 10.0*NSEC_PER_SEC, 0*NSEC_PER_SEC);
 dispatch_source_set_event_handler(timerSource, ^{
     if(queue)
         [self findlocation];
 });
 dispatch_resume(timerSource);