2014-04-24 54 views
1

我正在进行报警应用。我最近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]; 
    } 
+0

'beginBackgroundTaskWithExpirationHandler:'是不是意味着这一点,是为了完成一些冗长像上传图片到服务器这样的任务。您无法使用'beginBackgroundTaskWithExpirationHandler:'在后台保留应用程序。您必须使用'UILocalNotification'作为警报,否则可能会/会让您的应用程序被拒绝。 – rckoenes

回答

2

由于应用程序不应该像这样工作。如果你希望你的应用程序在后台连续播放声音,那么它应该被注册为一个特殊的应用程序。像音乐播放器一样。如果您的应用只提供告警,那么您必须使用本地通知来从应用中“触发”声音。通知可以用30秒的声音播放,并以特定的时间间隔重播。 3分钟后,您的应用程序将无法播放,因为您的“后台”执行时间已过。那就是苹果让你的应用程序在后台运行的时间,以便在用户关闭你的应用程序之后做任何你必须做的事情。

这是在您想要在背景上继续播放声音的情况下。请注意,如果播放停止,您的应用程序将停止。

https://developer.apple.com/library/ios/qa/qa1668/_index.html#//apple_ref/doc/uid/DTS40010209

如果您想使用声音通知(30秒最大)在这里看到:

https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/IPhoneOSClientImp.html

+0

Thnaks为您的答复,但它不适合我。我在后台使用计时器。我认为我面临这个问题。 –

+0

你根本不应该使用定时器,你需要安排一个本地通知。 – Pochi

+0

只有30秒没有本地通知。我想充分响。如果用户停止,那么它会停止 –