ios nstimer 在后台运行以检查闹钟时间是否等于当前时间并播放音频 [英] ios nstimer run in background for check alarm time is equal to current time and play audio

查看:17
本文介绍了ios nstimer 在后台运行以检查闹钟时间是否等于当前时间并播放音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用谷歌搜索了几个小时,但找不到任何信息,如果有什么方法可以在应用程序在后台运行时保持正在运行的 NSTimer 处于活动状态?请帮我 ,当应用程序进入后台然后运行一个功能,每秒检查闹钟时间是否等于当前时间,然后播放音频并打开关闭闹钟页面如果有人知道警报应用程序开源并在后台运行,请与我分享我在这里尝试这个,但这不起作用

I googled it for hours, but could not find any information if there is any way to keep a running NSTimer active when the app is running in the background ? Please Help me , when app is go to background then one function run and each sec check alarm time is equal to current time , then play audio and open close alarm page If anybody know alarm app open source and its run on background please share with me Here i try this , but this is not working

- (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";

                }
            }

        }
    }


}

请帮帮我,

推荐答案

NSTimer 不会在后台运行.并且使用 beginBackgroundTaskWithExpirationHandler 不会解决您的问题.使用后台任务,您的应用最多可以运行 10 分钟,然后就会被杀死

NSTimer will not run in background. and using beginBackgroundTaskWithExpirationHandler won't save your problem. with background task your app can run max 10 min and then will get killed

你应该使用 UILocalNotification

UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = dateTime;
localNotification.alertBody = [NSString stringWithFormat:@"Alert Fired at %@", dateTime];
localNotification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

用 NSCalendar 计算时间并添加这一行:

calculate time with NSCalendar and add this line:

localNotification.repeatInterval = NSDayCalendarUnit;

这篇关于ios nstimer 在后台运行以检查闹钟时间是否等于当前时间并播放音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆