【问题标题】:How to make my application run in background?如何让我的应用程序在后台运行?
【发布时间】:2012-08-20 06:43:48
【问题描述】:

我正在开发一个警报应用程序。我在我的应用程序中使用 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;
    }
}

提前致谢。

【问题讨论】:

  • @SavaMazăre 请在编辑 cmets 时尽量保持礼貌和建设性。你说这张海报的语言比他们说英语更好吗?如果没有,请不要评论他们的语言使用,并坚持在评论中描述您的编辑。
  • @BJH:坏主意。所描述的“解决方案”违反了应用商店规则。
  • 谢谢@KevinBallard...以为是重复的。

标签: iphone background nstimer


【解决方案1】:

警报使用预定的本地notifications

【讨论】:

    【解决方案2】:

    你不能这样做,至少在公共 API 和应用商店规则的范围内不能这样做。

    【讨论】:

    • 如果有任何其他方法可以做到这一点,我需要每秒钟调用 -(void)onTimeEvent 方法,甚至应用程序进入最小化(睡眠模式或我们进入其他应用程序)并运行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-27
    • 1970-01-01
    • 2015-01-04
    • 2016-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多