我正在开发一个警报应用程序。我在我的应用程序中使用NSTimer。计时器工作得很好。但是当我最小化应用程序时,计时器停止工作。如何在应用程序关闭或最小化时使其工作?
这是我的代码
- (void) alarmSaveButtonAction:(id)sender {
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self
selector:@selector(onTimerEvent)
userInfo:nil repeats:YES];
}
-(void)onTimerEvent
{
NSLog(@"saravanan");
NSDate *now = [NSDate new];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"hh:mm:ss a";
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSLog(@"The Current Time is %@",[dateFormatter stringFromDate:now]);
NSString *string2 = [NSString stringWithFormat:[dateFormatter stringFromDate:now]];
NSLog(@"currenttime %@", string2);
[dateFormatter release];
Resource *resourceLoader = [[Resource alloc] init];
NSDictionary *prefDic = [resourceLoader getPlistDataAsDictionary:@"preference"];
NSDate *selectedDateTime = [[NSDate alloc]init];
selectedDateTime = [prefDic objectForKey:@"alarmtime"];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
dateFormatter1.dateFormat = @"hh:mm:ss a";
[dateFormatter1 setTimeZone:[NSTimeZone systemTimeZone]];
NSLog(@"saravanan date %@",[dateFormatter1 stringFromDate:selectedDateTime]);
NSString *string1 = [NSString stringWithFormat:[dateFormatter1 stringFromDate:selectedDateTime]];
NSLog(@"currenttime %@", string1);
[dateFormatter1 release];
if ([string1 isEqualToString:string2])
{
NSLog(@"alarm scheduld");
[self scheduledNotification];
[timer invalidate];
timer = nil;
}
}
提前致谢。