@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
int _count;
UIBackgroundTaskIdentifier _taskId;
NSTimer *_timer;
}
//程序已经进入后台
- (void)applicationDidEnterBackground:(UIApplication *)application
{
//开启一个后台任务
_taskId=[application beginBackgroundTaskWithExpirationHandler:^{
//结束指定的任务
[application endBackgroundTask:_taskId];
}];
_timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
}
#pragma mark -- Action
-(void)timerAction:(NSTimer *)timer
{
_count++;
if(_count==500)//后台任务的生命同期为600s
{
UIApplication *application=[UIApplication sharedApplication];
//结束旧的后台任务
[application endBackgroundTask:_taskId];
//开启一个新的后台任务
_taskId=[application beginBackgroundTaskWithExpirationHandler:^{
[application endBackgroundTask:_taskId];
}];
_count=0;
}
}
{
int _count;
UIBackgroundTaskIdentifier _taskId;
NSTimer *_timer;
}
//程序已经进入后台
- (void)applicationDidEnterBackground:(UIApplication *)application
{
//开启一个后台任务
_taskId=[application beginBackgroundTaskWithExpirationHandler:^{
//结束指定的任务
[application endBackgroundTask:_taskId];
}];
_timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
}
#pragma mark -- Action
-(void)timerAction:(NSTimer *)timer
{
_count++;
if(_count==500)//后台任务的生命同期为600s
{
UIApplication *application=[UIApplication sharedApplication];
//结束旧的后台任务
[application endBackgroundTask:_taskId];
//开启一个新的后台任务
_taskId=[application beginBackgroundTaskWithExpirationHandler:^{
[application endBackgroundTask:_taskId];
}];
_count=0;
}
}