場景:
1浮梢,當(dāng)app在后臺時跛十,定時關(guān)閉音樂。
2秕硝,藍(lán)牙交互芥映;
- (void)bgTimerCount:(NSTimer *)timer
{
count++;
if(count ==500) {
//結(jié)束后臺任務(wù)
[[UIApplication sharedApplication] endBackgroundTask:bgTaskId];
//開啟一個新的后臺
bgTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];
}
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
DLog(@"applicationDidEnterBackground");
//啟動一個后臺任務(wù)[默認(rèn)生命周期600s]
//當(dāng)該任務(wù)超時回調(diào)該block塊
bgTaskId = [application beginBackgroundTaskWithExpirationHandler:^(void){
//結(jié)束該任務(wù)
[application endBackgroundTask:bgTaskId];
}];
[NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(bgTimerCount:)
userInfo:nil
repeats:YES];
}