【发布时间】:2010-12-16 16:19:34
【问题描述】:
我正在为 iOS 4 编写一个应用程序,该应用程序需要在后台以定期(用户指定的)间隔播放声音(或振动)。我不想使用本地通知,因为不需要出现可点击的警报。
当我的应用切换到后台时,会触发此代码(来自 Apple 的文档):
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.
NSLog(@"I am now in the background");
// Here I need something like this:
[self performSelector:@selector(myMethod) withObject:nil afterDelay:5.0];
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
});
[self performSelector: ...] 行在应用程序切换到后台时执行,但在应用程序切换回前台之前不会调用 myMethod 方法(不是在 5 秒内,如在 afterDelay: 中设置的那样范围)。那么,如何在应用仍在后台时调用该方法?
【问题讨论】:
-
您能否告知您的应用程序将在后台保留多长时间?我读到它最多 10 分钟.. 是对还是错?
标签: loops audio ios4 background multitasking