IOS在后台每隔一段时间执行一下
步骤:
1.在info.plist里加入UIBackgroundModes键,其值为数组,数组之一为voip字符串:
<key>UIBackgroundModes</key><array><string>voip</string></array>
2.在程序启动的时候调用- (void)setupBackgroundHandler函数,函数体如下:
#pragma mark - VoIP - ( void )setupBackgroundHandler { if ( UIUDeviceIsBackgroundSupported() )
if ( [[UIApplication sharedApplication] setKeepAliveTimeout:600 handler: ^ { [ self requestServerHowManyUnreadMessages]; } ] ) { UDLog(@ "Set Background handler successed!" ); } else { //failed UDLog(@ "Set Background handler failed!" ); } } else { UDLog(@ "This Deviece is not Background supported." ); } } - ( void )requestServerHowManyUnreadMessages { UIApplication* app = [UIApplication sharedApplication]; if ([app applicationState] == UIApplicationStateBackground) { NSArray * oldNotifications = [app scheduledLocalNotifications]; if ([oldNotifications count] > 0) [app cancelAllLocalNotifications]; UILocalNotification* alarm = [[[UILocalNotification alloc] init] autorelease]; if (alarm) { alarm.fireDate = [ NSDate dateWithTimeIntervalSinceNow:15]; alarm.timeZone = [ NSTimeZone defaultTimeZone]; alarm.repeatInterval = 0; alarm.soundName = UILocalNotificationDefaultSoundName; alarm.alertBody = @ "Time to request MOA2 Server!" ; [app scheduleLocalNotification:alarm]; } } else if ([app applicationState] == UIApplicationStateActive) { UIAlertView *alertView = [[[UIAlertView alloc] init] autorelease]; [alertView setTitle:@ "alert" ]; [alertView setMessage:@ "Time to request MOA2 Server!" ]; [alertView addButtonWithTitle: NSLocalizedString (@ "cancel" , nil )]; [alertView setDelegate: nil ]; [alertView show]; } } |
解说:
- (BOOL)setKeepAliveTimeout:(NSTimeInterval)timeout handler:(void (^)(void))keepAliveHandler
函数功能:app每隔timeout唤醒一次。
0.要成功调用该函数,就必须在Info.plist里设UIBackgroundModes键的array值之一voip字符串.
1.timeout必须>=600
2.唤醒app的时间间隔是不精准的。
3.唤醒后只有10秒执行时间。即handler里的代码要在10秒类执行完。10秒后app再次被阻塞。
(可以用-backgroundTimeRemaining属性来返回剩余时间)
4.该函数成功调用后,在程序生命周期内有效。
该函数的效果在回到前台的状况下,依然有效。(因此可以把它当timer使.)
5.clearKeepAliveTimeout函数用来清除handler。
IOS在后台每隔一段时间执行一下的更多相关文章
- C#实现每隔一段时间执行代码(多线程)
总结以下三种方法,实现c#每隔一段时间执行代码: 方法一:调用线程执行方法,在方法中实现死循环,每个循环Sleep设定时间: 方法二:使用System.Timers.Timer类: 方法三:使用Sys ...
- delphi中我用定时器每隔一段时间执行操作
delphi中,我用定时器每隔一段时间执行数据库插入及更新工作!adoquery.close;adoquery.sql.cleare;adoquery.connection:=con1;adoquer ...
- 【C#-多线程】实现每隔一段时间执行代码(多线程) 3种定时器
总结以下三种方法,实现c#每隔一段时间执行代码: 方法一:调用线程执行方法,在方法中实现死循环,每个循环Sleep设定时间: 方法二:使用System.Timers.Timer类: 方法三:使用Sys ...
- 每隔一段时间执行一次函数。window.setTimeout
timer2 = window.setTimeout("showTaxi()", 30000);//30秒从后台获取一次数据,显示在地图上. 原来试过 setInterval . ...
- 如何在JAVA中每隔一段时间执行一段程序
可以用线程来做,每隔几秒开一个线程代码如下 public void runTask() { final long timeInterval = 120000;// 两分钟运行一次 final Thre ...
- js每隔一段时间执行函数
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- js setInterval每隔一段时间执行一次
js setInterval每隔一段时间执行一次setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式.setInterval() 方法会不停地调用函数,直到 clearI ...
- 隔一段时间应用就会挂掉(进程在,但停止响应,也无log输出),必须重启tomcat
此处是转载的 是给自己做的备注 问题:隔一段时间应用就会挂掉(进程在,但停止响应,也无log输出),必须重启tomcat 原因查找:由于tomcat自身log中并无错误产生,磁盘空间足够,读写也正常 ...
- flask中使用ajax 处理前端请求,每隔一段时间请求不通的接口,结果展示同一页面
需求: flask中使用ajax 处理前端请求,每隔一段时间请求不通的接口,结果展示同一页面 用到 setTimeout方法,setTimeout(function(){},1000):setTime ...
随机推荐
- 关于调用系统照相机Activity被销毁问题解决
<activity android:name=".visitplan_finish" android:co ...
- linux的终端,网络虚拟终端,伪终端(转)
转自http://www.xuebuyuan.com/877887.html 2013年09月07日 ⁄ 综合 ⁄ 共 4047字 ⁄ 字号 小 中 大 ⁄ 评论关闭 Linux上许多网络服务应用,如 ...
- WindowListener中的windowClosed方法不执行的问题。
1.在正常情况下windowClosed方法不执行: 2.调用dispose方法,windowClosed方法会执行.例如:在windowClosing方法中执行dispose方法,windowClo ...
- V$SESSION_LONGOPS
对大部分DBA来说,V$SESSION_LONGOPS视图都不会陌生,以前在面试的时候,也有一些企业会问到如何查询数据库中运行时间比较长的SQL,就可以通过这个视图来查看.V$SESSION_LONG ...
- 菜鸟笔记之java中方法使用
N!!!java中无参无返回值方法的使用 1,定义方法 eg: public void show(){ System.out.println("HelloWorld!") } -- ...
- 06_WebService与Socket的区别
[区别] 区别1. Socket是基于TCP/IP的传输层协议. WebService是基于HTTP协议传输数据的,HTTP是基于TCP的应用层协议. 区别2. WebService采用了基于HTTP ...
- height为100%的问题
问题描述 很多同学,对于设置div 的高度为100%时,有疑惑. 设置div 的高度为100%,意思是此 div 的高度 铺满父元素. 那么 怎么使 div 铺满浏览器屏幕? 代码 <!DOCT ...
- 01_C语言基础
内容提要: 1. C语言概述2. 数据类型.运算符与表达式3. C语言程序结构 4. VC6.0使用练习 知识详解01:C语言的历史 1. C语言与其它语言比较 汇编语言: (1).可直接对硬件进行操 ...
- SSH框架jar神包
SSH JAR包链接(https://zhidao.baidu.com/share/ac8b1389bac84023d7442cad88f3933c.html)
- 使用自定义 jQuery 插件的一个选项卡Demo
前几天闲着没事,想着编写一个 jQuery 插件,或许这将是一个美好的开始. 这里是html页面: <!DOCTYPE html> <html lang="en" ...