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,
),
^{ 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, ), ^{
while(TRUE)
{
[NSThread sleepForTimeInterval:]; //编写执行任务代码
[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, ), ^{
dispatch_async(dispatch_get_main_queue(), ^{
if (bgTask != UIBackgroundTaskInvalid)
{
bgTask = UIBackgroundTaskInvalid;
}
});
});
}
Delphi IOS 后台定时器的更多相关文章
- delphi IOS 后台状态保存
FormSaveState procedure TFrm.FormSaveState(Sender: TObject);begin end; http://stackoverflow.com/ques ...
- Delphi IOS 蓝牙锁屏后台运行
Delphi IOS 后台运行 同样的程序,编译成android,锁屏后继续运行正常,蓝牙通讯正常,但在IOS下锁屏后程序的蓝牙就中断通讯了? IOS的机制就是这样,锁屏就关闭了. 音乐播放器是怎么做 ...
- iOS 后台处理
iOS 后台处理的常见用途 1.进入后台时候删除资源:应用处于挂起状态的时候所占用的资源越少,该应用被iOS终止的风险就越低.通过从内存中清理那些易于重新创建的资源,可以增加应用驻留内存的机会,因此可 ...
- iOS后台定位实现
iOS后台定位实现 (2013-01-24 16:43:12) 工作中碰到一个定位的应用场景:app需要在后台运行,实时上传用户地理位置. 苹果对iOS的规范性在提升了app的品质的同时也 ...
- iOS 后台运行 类型
iOS后台运行,需要有特定的类型才可以进行.这些内容并不是一直不变的,苹果也在逐步的更新这些内容. 本文内容是2015年11月03日时苹果支持的后台运行类型. 这是官方连接地址 其中较为重要的是下面这 ...
- iOS后台运行
http://www.cocoachina.com/bbs/read.php?tid=149564 文一 我从苹果文档中得知,一般的应用在进入后台的时候可以获取一定时间来运行相关任务,也就是说可以在后 ...
- IOS 设置定时器
IOS 设置定时器 自动滚动视图 定时发送坐标信息 即时显示 时钟 NSTimer *timer; - (void)start {//1second 调用一次 timer = [NSTimer sc ...
- IOS 后台执行
在IOS后台执行是本文要介绍的内容,大多数应用程序进入后台状态不久后转入暂停状态.在这种状态下,应用程序不执行任何代码,并有可能在任意时候从内存中删除.应用程序提供特定的服务,用户可以请求后台执行时间 ...
- iOS 后台运行实现 --备用
文一 我从苹果文档中得知,一般的应用在进入后台的时候可以获取一定时间来运行相关任务,也就是说可以在后台运行一小段时间. 还有三种类型的可以运行在后以,1.音乐2.location 3.voip 文二 ...
随机推荐
- Codeforces 914H Ember and Storm's Tree Game 【DP】*
Codeforces 914H Ember and Storm's Tree Game 题目链接 ORZ佬 果然出了一套自闭题 这题让你算出第一个人有必胜策略的方案数 然后我们就发现必胜的条件就是树上 ...
- CDQ分治--用时间降维的美丽算法
CDQ分治–用时间降维的美丽算法 CDQ分治,网上的阐述很多,太专业性的文字我就不赘述,这里指谈谈自己的感受 还是%一下CDQ大神的论文 CDQ分治的主要想法就是降维(比如三维问题降维到二维问题),并 ...
- Django 向数据表中添加字段方法
在模型order中添加字段discount字段,并给予初始值0 方法: 先在models.py中修改模型 添加 discount = models.DecimalField(max_digits=8, ...
- iOS 信号量解决-网络异步请求的数据同步返回问题
有那么一个场景如下 +PayWithBlock:(NSString*(^)(NSString *message)) block; 如果 block 返回是同步的那是没有问题的,但是如果block 内容 ...
- 接口测试框架——第四篇-url、excel内容等
到现在为止,发送邮件(email_module).读excel(excel_module).发送requests(requests_module).常量(setting)我们都已经完成了,看看第一篇中 ...
- Windows下Redis的使用
Redis介绍 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,和Memcached类似,它支持存储的value类型相对更多,包括st ...
- 百度编辑器ueditor更改图片默认编辑工具
点击图片编辑器默认出现的工具 需求是点击图片不出现默认样式,而是在编辑器外出现图片属性编辑框,需求效果如下 我的做法是在css里面将默认出现的工具隐藏,然后直接在ueditor.all.js里面修改S ...
- nginx+uwsgi 和nginx+gunicorn区别、如何部署
[线上环境部署Django,nginx+uwsgi 和nginx+gunicorn,这两种方案,应该如何选择?] 大家是采用的何种部署方式? 第一种,高并发稳定一点 我们公司使用的是nginx+gun ...
- 安装ecb
mac emacs上安装ecb,通过elpa折腾得要死,死活无法使用. 解决办法:下载https://github.com/alexott/ecb,添加路径,(require 'ecb),直接ok.
- C++中const使用注意要点(一)
最近看<C++编程思想>发现自己的基础确实不牢固,也想起了以前写代码时也因为const的事情浪费过时间,这里总结下几个要点. 首先说下内部链接和外部链接. 当一个cpp文件在编译时,预处理 ...