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 文二 ...
随机推荐
- 【javascript】利用jquery ajaxPrefilter防止ajax重复提交
利用jquery ajaxPrefilter中断请求 var pendingRequests = {}; $.ajaxPrefilter(function( options, originalOpti ...
- ASP.NET WebForm 与 IE10、IE11
最近维护公司一个烂项目.NET4.0 Asp.Net WebForm(在MVC5泛滥的今天还有用WebForm的(⊙o⊙)…) IE10.IE11下aspx中大表单提交不上去,报_DoPostBack ...
- IOI2002 POJ1054 The Troublesome Frog 讨厌的青蛙 (离散化+剪枝)
Description In Korea, the naughtiness of the cheonggaeguri, a small frog, is legendary. This is a we ...
- Codeforces1070 2018-2019 ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred)总结
第一次打ACM比赛,和yyf两个人一起搞事情 感觉被两个学长队暴打的好惨啊 然后我一直做傻子题,yyf一直在切神仙题 然后放一波题解(部分) A. Find a Number LINK 题目大意 给你 ...
- 关于上架AppStore最后一步的“出口合规信息”、“内容版权”、“广告标识符”的选择(转)
这里只当作记录:http://blog.csdn.net/ashimar_a/article/details/51745675
- C#飞行棋游戏
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- BZOJ1495 [NOI2006]网络收费
题意 传送门 MY市NS中学,大概是绵阳市南山中学. 分析 参照Maxwei_wzj的题解. 因为成对的贡献比较难做,我们尝试把贡献算到每一个叶子节点上.我们发现按照题目中的收费方式,它等价于对于每棵 ...
- java创建多线程&创建进程
概述 并发和并行是即相似又有区别: 并行:指两个或多个事件在同一时刻发生: 并发:指两个或多个事件在同一时间段内发生. 进程是指一个内存中运行中的应用程序.每个进程都有自己独立的一块内存空间,一个应用 ...
- 什么是虚拟环境、为什么使用虚拟环境、Anaconda创建、激活、退出、删除虚拟环境
一.虚拟环境 virtual environment 它是一个虚拟化,从电脑独立开辟出来的环境.通俗的来讲,虚拟环境就是借助虚拟机docker来把一部分内容独立出来,我们把这部分独立出来的东西称作“容 ...
- 纯 as3 项目中引用 fl 包下的类
如果安装了 Flash IDE, 将下面的文件添加到项目的 libs 中即可:D:\Program Files\Adobe\Adobe Flash CS6\Common\Configuration\A ...