【发布时间】:2014-08-20 13:39:08
【问题描述】:
我用谷歌搜索了几个小时,但是如果有任何方法可以在应用程序在后台运行时保持正在运行的 NSTimer 处于活动状态,但找不到任何信息?请帮我 , 当应用程序进入后台然后运行一个功能并且每秒检查警报时间等于当前时间,然后播放音频并打开关闭警报页面 如果有人知道警报应用程序开源及其在后台运行,请与我分享 在这里我试试这个,但这不起作用
- (void)applicationDidEnterBackground:(UIApplication *)application
{
inBackground=YES;
UIApplication* app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
while (inBackground == YES) {
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self
selector:@selector(checkAlert) userInfo:nil repeats:YES];
}
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
});
}
-(void)checkAlert
{
NSLog(@"Chalaaaaaa");
NSString *checkAlarmOnOff = [defaults stringForKey:@"setAlarm"];
// Switch Alarm From Setting page
NSString *SwitchAlarm=[defaults stringForKey:@"setSwitchAlarm"];
if([SwitchAlarm isEqualToString:@"ON"])
{
if([checkAlarmOnOff isEqualToString:@"YES"])
{
if(![alarmRun isEqualToString:@"Yes"])
{
NSString *getAlarmTime = [defaults stringForKey:@"setAlarmTime"];
NSLog(@"AA-%@",getAlarmTime);
NSLog(@"BB-%@",globalClockTime);
if([getAlarmTime isEqualToString:globalClockTime])
{
[self MusicAction];
[_audioPlayer play];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alarm" message:@"Alarm On" delegate:self cancelButtonTitle:@"Off" otherButtonTitles:nil, nil];
[alert show];
alarmRun=@"Yes";
}
}
}
}
}
请帮帮我,
【问题讨论】:
-
if([getAlarmTime isEqualToString:globalClockTime]) 似乎有问题。不能保证时间会相等。尝试创建一个 if 大于关系。
标签: ios alarm background-service