GCD之后台程序运行
点击Home键进入后台时进行计时,直到从新启动,超过三分钟启动手势
// // AppDelegate.m // GCDDown // // Created by City--Online on 15/4/21. // Copyright (c) 2015年 CYW. All rights reserved. // #import "AppDelegate.h" @interface AppDelegate () @property (assign, nonatomic) UIBackgroundTaskIdentifier backgroundUpdateTask; @property(nonatomic,assign)NSInteger count; @property(nonatomic,strong)NSTimer *timer; @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. return YES; } - (void)applicationWillResignActive:(UIApplication *)application { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } - (void)applicationDidEnterBackground:(UIApplication *)application { NSLog(@"进入后台"); [self beingBackgroundUpdateTask]; // 在这里加上你需要长久运行的代码 self.timer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updatecount) userInfo:nil repeats:YES]; [self.timer fire]; [self endBackgroundUpdateTask]; } -(void)updatecount { self.count++; } - (void)beingBackgroundUpdateTask { self.backgroundUpdateTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ [self endBackgroundUpdateTask]; }]; } - (void)endBackgroundUpdateTask { [[UIApplication sharedApplication] endBackgroundTask: self.backgroundUpdateTask]; self.backgroundUpdateTask = UIBackgroundTaskInvalid; } - (void)applicationWillEnterForeground:(UIApplication *)application { NSLog(@"进入前台"); NSLog(@"%ld",self.count); self.count=0; [self.timer invalidate]; } - (void)applicationDidBecomeActive:(UIApplication *)application { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } - (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } @end
GCD之后台程序运行的更多相关文章
- 掌握GCD以及后台永久运行的代码 (使用GCD处理后台线程和UI线程的交互)
一个例子: 在iPhone上做一个下载网页的功能,就是:在iPhone上放一个按钮,单击按钮时,显示一个转动的圆圈,表示正在进行下载,下载完成后,将内容加载到界面上的一个文本控件上. 使用GCD前: ...
- jenkins 后台程序运行
# 背景 jenkins持续集成,需要任务后台执行(nohup执行)结果发现jenkins的job执行完后,看不到运行的进程 # 步骤 原因就是这么一个情况:Jenkins任务结束时候自动关掉了所有的 ...
- JAVA WEB 前台实时监控后台程序运行
基本思路: 1. 操作状态在类中以静态变量方式(或公共类存储公共变量方式,SESSION方式.COOKIE方式)存在 2. 前台采用AJAX方式激发后台进行业务逻辑操作,并实时更新操作状态信息 3. ...
- 安卓利用Handlers,AsyncTask和Loaders运行后台程序
安卓的用户界面线程(user interface thread) 1.1 主线程 安卓修改用户界面并从一个单一用户界面线程中处理输入事件,这个线程也被称作主线程(main thread) Androi ...
- Swift - 让程序挂起后,能在后台继续运行任务
1,程序的挂起和退出 由于iOS设备资源有限.当用户点击了home键,或者另一个应用程序启动了.那么原先那个程序便进入后台被挂起,不是退出,只是停止执行代码,同时它的内存被锁定.当应用程序恢复时,它会 ...
- 程序自启动及后台持续运行的研究(voip和GPS方式)
1,voip方式 1)首先要修改应用的plist配置,添加下面的设置: Application does not run in background: NO Requ ...
- 让java程序在后台一直执行(例如putty关闭后后台程序继续运行)
如果在终端中执行java -jar xxx.jar&命令,当终端关闭时,xxx.jar也会同时结束运行,但是如果执行nohup java -jar xxx.jar&命令,则程序会在后台 ...
- ios后台程序持续运行方法
iOS系统的资源是有限的,应用程序在前台和在后台的状态是不一样的.在后台时,程序会受到系统的很多限制,这样可以提高电池的使用和用户体验.但是有很多社交类的软件,如果因为它在后台就不能刷新到新的数据的话 ...
- 【iOS】swift 让程序挂起后,能在后台继续运行任务
1,程序的挂起和退出 由于iOS设备资源有限.当用户点击了home键,或者另一个应用程序启动了.那么原先那个程序便进入后台被挂起,不是退出,只是停止执行代码,同时它的内存被锁定.当应用程序恢复时,它会 ...
随机推荐
- java数据库编程(未整理完,待续)
java使用数据库可以借助jdbc这个中间媒介.本文将介绍如何使用jdbc连接数据库,数据库的基本操作和jdbc的事物处理. 1 连接数据库 一般java连接数据库,都有几个步骤: 0.导入相应的驱动 ...
- 加密算法比较3DES AES RSA ECC MD5 SHA1等
加 密算法通常分为对称性加密算法和非对称性加密算法,对于对称性加密算法,信息接收双方都需事先知道密匙和加解密算法且其密匙是相同的,之后便是对数据进行 加解密了.非对称算法与之不同,发送双方A,B事先均 ...
- day06_雷神_面向对象初识
day_06 递归函数 自己用自己.一般递归100多次,都没有解决的问题,放弃递归. count = 0 def func1(): global count count += 1 print(coun ...
- 《A computer-aided healthcare system for cataract classification and grading based on fundus image analysis》学习笔记
Abstract This paper presents a fundus image analysis based computer aided system for automatic class ...
- ip网段变更
背景 公司网络跟集团靠拢,先走第一步:IP网段变更.从XX网段切换到OO网段 方法 1. 准备工作 a. 保证IPMI连接正常 b. 获得新IP并核对对应主机名.旧IP是否相符 2. 确认网卡名称 # ...
- ajax调用WebMethed返回处理请求时出错
ajax post调用WebMethed报错,返回的信息如下: {“Message”:“处理请求时出错”,“StackTrace”:“”,“ExceptionType”:“”} 查了一下WebMeth ...
- MariaDB MyCat实现读写分离(15)
MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可MariaDB的目的是完全兼容MySQL,包括API和命令行,MySQL由于现在闭源了,而能轻松成为MySQ ...
- linux apache+php+mysql安装及乱码解决办法
1.乱码解决方法 首先确认mysql数据库字符集设置正确,php页面字符设置正确,之后修改apache配制文件http.conf 注释掉以下字符 AddDefaultCharset UTF-8 此为乱 ...
- iOS数据持久化--数据库
一.简介 1.iOS常用的5中存储方式 (1)plist (2)preference(用户属性) (3)归档 (4)数据库 (5)core data 其中(1)(2) (3) 都只能存储小型的数据,因 ...
- Django中安装搜索引擎方法。
全文检索 全文检索不同于特定字段的模糊查询,使用全文检索的效率更高,并且能够对于中文进行分词处理. haystack:全文检索的框架,支持whoosh.solr.Xapian.Elasticsearc ...