博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Delphi IOS 后台定时器
阅读量:4953 次
发布时间:2019-06-12

本文共 4015 字,大约阅读时间需要 13 分钟。

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

beginBackgroundTaskWithExpirationHandler

-(void)applicationDidEnterBackground:(UIApplication*)application{UIApplication*app = [UIApplication sharedApplication];__blockUIBackgroundTaskIdentifier 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;                  }              });          });      }

 

转载于:https://www.cnblogs.com/cb168/p/5137457.html

你可能感兴趣的文章
ROS的脚本多拨
查看>>
zabbix4.0 相关的拓扑图及centos的虚拟配置
查看>>
金蝶报错事项
查看>>
PLC实现积分的计算方法
查看>>
PLC中相关量的斜坡控制
查看>>
PLC中m法计算电机转速
查看>>
一起学习设计模式
查看>>
黑科技之三元环讲解
查看>>
supervisor安装及配置
查看>>
win10下maven的安装与配置
查看>>
css ie hack整理
查看>>
hadoop源码学习(二)之ZooKeeper
查看>>
英文字母和中文汉字在不同字符集编码下的字节数
查看>>
nodejs v4.4.5在windows下的安装
查看>>
第二阶段冲刺第十天
查看>>
ArrayList源码
查看>>
在VC项目中附加包含目录
查看>>
使用Swing的JSpinner组件设置日期时间选择器
查看>>
openssl 在php里
查看>>
Asp.NET MVC JSON序列化问题
查看>>