Ios 当警报在后台运行时,3分钟后警报未响起
我正在开发一个报警应用程序。过去两周我遇到了一个问题。我的问题是:我的应用程序正在后台运行。我第一次安装应用程序并设置警报并关闭应用程序时。如果报警时间距离当前时间超过3分钟,则其未响铃表示3分钟后报警在后台过程中未响铃。如果应用程序已打开,则警报正在工作 这是我的代码:Ios 当警报在后台运行时,3分钟后警报未响起,ios,Ios,我正在开发一个报警应用程序。过去两周我遇到了一个问题。我的问题是:我的应用程序正在后台运行。我第一次安装应用程序并设置警报并关闭应用程序时。如果报警时间距离当前时间超过3分钟,则其未响铃表示3分钟后报警在后台过程中未响铃。如果应用程序已打开,则警报正在工作 这是我的代码: self->bgTask = 0; NSAssert(self->bgTask == UIBackgroundTaskInvalid, nil); bgTask = [application b
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];
}
因为应用程序不应该这样工作。如果你想让你的应用程序在后台连续播放声音,那么它应该注册为专门播放声音的应用程序。就像一个音乐播放器。如果你的应用程序只发送警报,那么你必须使用本地通知“触发”应用程序发出的声音。该通知可以以30秒的声音播放,并以一定的间隔重新播放。你的应用程序在3分钟后无法播放,因为你的“后台”执行时间已过期。这是苹果让你的应用程序在后台运行的时候,在用户关闭你的应用程序后,你可以做任何你必须做的事情 这适用于您希望在背景上继续播放声音的情况。请注意,如果播放停止,您的应用程序将停止 如果要使用声音通知(最多30秒),请参见此处:
beginBackgroundTaskWithExpirationHandler:
不是用于此,而是用于完成一些冗长的任务,如将图像上载到服务器。您不能使用beginBackgroundTaskWithExpirationHandler:
将应用程序保留在后台。您必须使用UILocalNotification
进行报警,否则您的应用可能会被拒绝。谢谢您的回复,但这对我不起作用。我在后台使用定时器。我想我面临这个问题。你根本不应该使用计时器,你需要安排一个本地通知。没有本地通知只有30秒。我想要全环。如果用户是停止,那么它将停止