//定时器后台运行代码↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓定时器后台运行代码//
/*定时器后台运行*/
NSError *setCategoryErr =nil;
NSError *activationErr =nil;
/*设置Audio Session的Category 一般会在激活之前设置好Category和mode。但是也可以在已激活的audio session中设置,不过会在发生route change之后才会发生改变*/
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &setCategoryErr];
/*激活Audio Session*/
[[AVAudioSession sharedInstance] setActive:YESerror: &activationErr];
//定时器后台运行代码↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑定时器后台运行代码//
- (void)applicationDidEnterBackground:(UIApplication*)application {
//定时器后台运行代码↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓定时器后台运行代码//
UIApplication* app = [UIApplication sharedApplication];
__block UIBackgroundTaskIdentifier bgTask;
/*注册一个后台任务,告诉系统我们需要向系统借一些事件*/
bgTask = [appbeginBackgroundTaskWithExpirationHandler:^{
dispatch_async(dispatch_get_main_queue(), ^{
if(bgTask !=UIBackgroundTaskInvalid)
{
/*销毁后台任务标识符*/
/*不管有没有完成,结束background_task任务*/
bgTask =UIBackgroundTaskInvalid;
}
});
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
if(bgTask !=UIBackgroundTaskInvalid)
{
/*销毁后台任务标识符*/
/*不管有没有完成,结束background_task任务*/
bgTask =UIBackgroundTaskInvalid;
}
});
});
//定时器后台运行代码↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑定时器后台运行代码//
}
网友评论