场景:
1,当app在后台时,定时关闭音乐。
2,蓝牙交互;

- (void)bgTimerCount:(NSTimer *)timer
{
count++;
if(count ==500) {
//结束后台任务
[[UIApplication sharedApplication] endBackgroundTask:bgTaskId];
//开启一个新的后台
bgTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];
}
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
DLog(@"applicationDidEnterBackground");
//启动一个后台任务[默认生命周期600s]
//当该任务超时回调该block块
bgTaskId = [application beginBackgroundTaskWithExpirationHandler:^(void){
//结束该任务
[application endBackgroundTask:bgTaskId];
}];
[NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(bgTimerCount:)
userInfo:nil
repeats:YES];
}
网友评论