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 后台定时器的更多相关文章

  1. delphi IOS 后台状态保存

    FormSaveState procedure TFrm.FormSaveState(Sender: TObject);begin end; http://stackoverflow.com/ques ...

  2. Delphi IOS 蓝牙锁屏后台运行

    Delphi IOS 后台运行 同样的程序,编译成android,锁屏后继续运行正常,蓝牙通讯正常,但在IOS下锁屏后程序的蓝牙就中断通讯了? IOS的机制就是这样,锁屏就关闭了. 音乐播放器是怎么做 ...

  3. iOS 后台处理

    iOS 后台处理的常见用途 1.进入后台时候删除资源:应用处于挂起状态的时候所占用的资源越少,该应用被iOS终止的风险就越低.通过从内存中清理那些易于重新创建的资源,可以增加应用驻留内存的机会,因此可 ...

  4. iOS后台定位实现

    iOS后台定位实现 (2013-01-24 16:43:12)     工作中碰到一个定位的应用场景:app需要在后台运行,实时上传用户地理位置.   苹果对iOS的规范性在提升了app的品质的同时也 ...

  5. iOS 后台运行 类型

    iOS后台运行,需要有特定的类型才可以进行.这些内容并不是一直不变的,苹果也在逐步的更新这些内容. 本文内容是2015年11月03日时苹果支持的后台运行类型. 这是官方连接地址 其中较为重要的是下面这 ...

  6. iOS后台运行

    http://www.cocoachina.com/bbs/read.php?tid=149564 文一 我从苹果文档中得知,一般的应用在进入后台的时候可以获取一定时间来运行相关任务,也就是说可以在后 ...

  7. IOS 设置定时器

    IOS 设置定时器  自动滚动视图 定时发送坐标信息 即时显示 时钟 NSTimer *timer; - (void)start {//1second 调用一次 timer = [NSTimer sc ...

  8. IOS 后台执行

    在IOS后台执行是本文要介绍的内容,大多数应用程序进入后台状态不久后转入暂停状态.在这种状态下,应用程序不执行任何代码,并有可能在任意时候从内存中删除.应用程序提供特定的服务,用户可以请求后台执行时间 ...

  9. iOS 后台运行实现 --备用

    文一 我从苹果文档中得知,一般的应用在进入后台的时候可以获取一定时间来运行相关任务,也就是说可以在后台运行一小段时间. 还有三种类型的可以运行在后以,1.音乐2.location 3.voip 文二 ...

随机推荐

  1. 去除inline-block元素间间距的N种方法(转)

    一.现象描述 真正意义上的inline-block水平呈现的元素间,换行显示或空格分隔的情况下会有间距,很简单的个例子: <input /> <input type="su ...

  2. phpcms后台主菜单不显示

    phpcms\modules\admin\templates\main.tpl.php 注释掉既可

  3. CodeForces - 258D:Little Elephant and Broken Sorting(概率DP)

    题意:长度为n的排列,m次交换xi, yi,每个交换x,y有50%的概率不发生,问逆序数的期望  .n, m <= 1000 思路:我们只用维护大小关系,dp[i][j]表示位置i的数比位置j的 ...

  4. HDU1496 Equations 卡时间第二题

    Consider equations having the following form: a*x1^2+b*x2^2+c*x3^2+d*x4^2=0 a, b, c, d are integers ...

  5. Spring 管理Filter和Servlet

    本文转载自:http://www.open-open.com/lib/view/open1417248512252.html 在使用spring容器的web应用中,业务对象间的依赖关系都可以用cont ...

  6. PHP获取客户端的IP、地理信息、浏览器、本地真实IP

    <?php header("Content-type:text/html;charset=utf-8"); // 作用获取客户端的ip.地理信息.浏览器.本地真实IP cla ...

  7. MySQL事物回滚

    #commit.rollback用来确保数据库有足够的剩余空间:#commi.rollback只能用于DML操作,即insert.update.delet;#rollback操作撤销上一个commit ...

  8. Matlab 之 FFT的理解和应用

    网上看了一些大牛的关于FFT的见解,加上自己的一点儿理解,针对以下这几个问题来加深对FFT的理解. 不知道大家有没有类似以下几点的困惑: 问题的提出 对于1秒钟输出的连续信号,使用采样率Fs不同,就会 ...

  9. Qt学习笔记(1) hello world

    Qt的简介: Qt是一个跨平台的C++ GUI库实现,原本只是以为它只提供一些图形接口,看来我还是低估了它,采用文档Qt学习之路2开始学习,不知道这个文档是不是有点老了,管他呢,先了解下. 搭建环境: ...

  10. SublimeText3搭建go语言开发环境(windows)

    SublimeText3搭建go语言开发环境(windows) 下载并解压:     Sublime Text Build 3021.zip注册:     尽量不要去破解    安装Package C ...