Delphi IOS 后台定时器

3.这里有一个问题,就是客户端是通过心跳来和服务端保持连接,心跳是由定时器触发的,当我退到后台以后,定时器方法被挂起,那么通过如下设置来在后台运行定时器

beginBackgroundTaskWithExpirationHandler

-
(void)applicationDidEnterBackground:(UIApplication
*)application{

UIApplication*
app = [UIApplication sharedApplication];

__block
UIBackgroundTaskIdentifier bgTask;

bgTask
= [app beginBackgroundTaskWithExpirationHandler:^{

dispatch_async(dispatch_get_main_queue(),
^{

if

(bgTask != UIBackgroundTaskInvalid)

{

bgTask
= UIBackgroundTaskInvalid;

}

});

}];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
0),
^{

dispatch_async(dispatch_get_main_queue(),
^{

if

(bgTask != UIBackgroundTaskInvalid)

{

bgTask
= UIBackgroundTaskInvalid;

}

});

});

}

 

2

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    
    testPeripheral = [[ViewController sharedInst] userGetCurrentPeri];
    
    NSLog(@"Saving all fobs... testPeripheral:%@", testPeripheral);
    
    /// _scanTimer=nil;
    
    //    NSError *error;
    //    if (![self.managedObjectContext save:&error])
    //    {
    //        NSLog(@"Saving failed: %@", error);
    //    }
    //entryBackgroundMode是定时器呼叫的函数
//    if (!_scanTimer /* &&[USER_DEFAULT boolForKey:KEY_BACKGROUND_OPEN]*/) {
//        NSLog(@"_scanTimer active ...");
//        _scanTimer = [NSTimer timerWithTimeInterval:1.0f target:self selector:@selector(entryBackgroundMode) userInfo:nil repeats:YES];
//        
//        [[NSRunLoop currentRunLoop] addTimer:_scanTimer forMode:NSDefaultRunLoopMode /* NSRunLoopCommonModes */];
//    }
    /// CLLocationManager * manager = [CLLocationManager new];
    
    __block UIBackgroundTaskIdentifier background_task;
    
    background_task = [application beginBackgroundTaskWithExpirationHandler:^ {
        [application endBackgroundTask: background_task];
        background_task = UIBackgroundTaskInvalid;
    }];
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        while(TRUE)
        {
            [NSThread sleepForTimeInterval:1];
            
            //编写执行任务代码
            [self entryBackgroundMode];
//entryBackgroundModeReadRSSI是定时器呼叫的函数
// _scanTimer = [NSTimer timerWithTimeInterval:0.10f target:self selector:@selector(entryBackgroundModeReadRSSI) userInfo:nil repeats:NO]; // [[NSRunLoop currentRunLoop] addTimer:_scanTimer forMode:NSDefaultRunLoopMode /* NSRunLoopCommonModes */]; } [application endBackgroundTask: background_task]; background_task = UIBackgroundTaskInvalid; }); }

 

//注入定时器  
   NSError *setCategoryErr = nil;  
   NSError *activationErr  = nil;  
   [[AVAudioSession sharedInstance]  
    setCategory: AVAudioSessionCategoryPlayback  
    error: &setCategoryErr];  
   [[AVAudioSession sharedInstance]  
    setActive: YES  
    error: &activationErr];  

    - (void)applicationDidEnterBackground:(UIApplication *)application  
    {  
        //后台继续运行定时器  
        UIApplication*   app = [UIApplication sharedApplication];  
        __block    UIBackgroundTaskIdentifier bgTask;  
        bgTask = [app beginBackgroundTaskWithExpirationHandler:^{  
            dispatch_async(dispatch_get_main_queue(), ^{  
                if (bgTask != UIBackgroundTaskInvalid)  
                {  
                    bgTask = UIBackgroundTaskInvalid;  
                }  
            });  
        }];  
          
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{  
            dispatch_async(dispatch_get_main_queue(), ^{  
                if (bgTask != UIBackgroundTaskInvalid)  
                {  
                    bgTask = UIBackgroundTaskInvalid;  
                }  
            });  
        });  
    }  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值