【发布时间】:2013-05-23 09:11:02
【问题描述】:
大家好,我需要在应用程序处于后台时调用一个方法。现在我正在尝试实现这一点,但从未调用过计时器。
- (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.timer = nil;
[self initTimer];
});
}
- (void)initTimer {
DebugLog(@"timer INIT!!!!!");
if (self.timer == nil) {
self.timer = [NSTimer scheduledTimerWithTimeInterval:0.3
target:self
selector:@selector(checkUpdates:)
userInfo:nil
repeats:YES];
}
}
- (void)checkUpdates:(NSTimer *)timer{
[UIApplication sharedApplication].applicationIconBadgeNumber++;
DebugLog(@"timer FIRED!!!!!");
[self initTimer];
}
当应用程序处于后台时,我需要每隔 N 分钟或秒调用一次方法 checkUpdates。请注意,当应用程序进入后台时会调用 initTimer,但不会调用计时器。
【问题讨论】: