我正在进行报警应用。我最近2周遇到了一个问题。我的问题是:我的应用程序正在后台运行。首次安装应用程序并设置闹钟&关闭应用程序。如果闹铃时间超过当前时间3分钟以上,则表示3分钟闹铃在后台过程中不响铃。如果应用程序处于打开状态,则警报正在工在后台运行3分钟后报警不响铃
这是我的代码:
self->bgTask = 0;
NSAssert(self->bgTask == UIBackgroundTaskInvalid, nil);
bgTask = [application beginBackgroundTaskWithExpirationHandler:^
{
NSLog(@"beginBackgroundTaskWithExpirat");
dispatch_async(dispatch_get_main_queue(),^
{
NSLog(@"dispatch_async");
[application endBackgroundTask:self->bgTask];
self->bgTask = UIBackgroundTaskInvalid;
});
}];
dispatch_async(dispatch_get_main_queue(),^
{
NSLog(@"dispatch_get_main_queue");
//Start BG Timer
[self Start_Update_Timer];
self->bgTask = UIBackgroundTaskInvalid;
});
// This is my timer for background running ..
-(void) Start_Update_Timer
{
//If timer is already running, stop the timer
if(self.callTimer)
{
[self.callTimer invalidate];
self.callTimer=nil;
}
//call the timer for every one sec
self.callTimer =[NSTimer scheduledTimerWithTimeInterval:60 target:self selector:@selector(update_Alarm_List) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:self.callTimer forMode:NSRunLoopCommonModes];
}
'beginBackgroundTaskWithExpirationHandler:'是不是意味着这一点,是为了完成一些冗长像上传图片到服务器这样的任务。您无法使用'beginBackgroundTaskWithExpirationHandler:'在后台保留应用程序。您必须使用'UILocalNotification'作为警报,否则可能会/会让您的应用程序被拒绝。 – rckoenes