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 文二 ...
随机推荐
- HDU2181 哈密顿绕行世界问题
解题思路:哈密顿环游世界问题.一道简单的题目,用回溯. #include<cstdio> #include<cstring> #include<algorithm> ...
- NFS,两台linux设置网络共享文件系统
NFS,MOUNT,UMOUNT 简介 NFS是Network File System的简写,网络文件系统.通过使用NFS,用户和程序可以像访问本地文件一样访问远端系统上的文件,根据权限的控制可以对N ...
- Jmeter简单的接口测试
1.新建线程组 2.编辑线程组信息 3.在线程组中添加HTTP信息头管理器 4.配置HTTP信息头管理器 参数格式配置 5.在线程组中添加HTTP请求 6.编辑HTTP请求信息 7.添加响应断言 8. ...
- Python编码规范和Python风格规范
一.原因 1.长期的工作中,发现大多数程序员的代码可读性差 2.不同的程序员之间的协作很重要,代码可读性必须很好 3.版本升级时,要基于源码升级 4.不友好的代码会影响python的执行效率 二.基于 ...
- win32鼠标和键盘相关函数
键盘相关函数:http://msdn.microsoft.com/en-us/library/windows/desktop/ms645530%28v=vs.85%29.aspx 鼠标相关函数:htt ...
- simulink使用system test测试 (matlab2014a)
simulink中有个工具system test,tools->systemtest->launch system test 打开.打开之后界面是这样的 选择insert->test ...
- hessian协议原理
Hessian 原理分析 一. 远程通讯协议的基本原理 网络通信需要做的就是将流从一台计算机传输到另外一台计算机,基于传输协议和网络 IO 来实现,其中传输协议比较出名的有 http . t ...
- redis在mac中的安装和启动
http://blog.csdn.net/chenshuai1993/article/details/51519384 http://www.jianshu.com/p/6b5eca8d908b
- DiscuzX 3. 3搭建和学习
Discuz!全局变量$_G详解 http://jingyan.baidu.com/article/cb5d610516048c005c2fe0c8.html UCenter uc_user_synl ...
- bzo1007 [HNOI2008]水平可见直线
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1007 所有可见直线形成下凸壳的样子.而且交点横坐标递增. (特殊判断平行线.但是按b从小到大 ...