点击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之后台程序运行的更多相关文章

  1. 掌握GCD以及后台永久运行的代码 (使用GCD处理后台线程和UI线程的交互)

    一个例子: 在iPhone上做一个下载网页的功能,就是:在iPhone上放一个按钮,单击按钮时,显示一个转动的圆圈,表示正在进行下载,下载完成后,将内容加载到界面上的一个文本控件上. 使用GCD前: ...

  2. jenkins 后台程序运行

    # 背景 jenkins持续集成,需要任务后台执行(nohup执行)结果发现jenkins的job执行完后,看不到运行的进程 # 步骤 原因就是这么一个情况:Jenkins任务结束时候自动关掉了所有的 ...

  3. JAVA WEB 前台实时监控后台程序运行

    基本思路: 1. 操作状态在类中以静态变量方式(或公共类存储公共变量方式,SESSION方式.COOKIE方式)存在 2. 前台采用AJAX方式激发后台进行业务逻辑操作,并实时更新操作状态信息 3.  ...

  4. 安卓利用Handlers,AsyncTask和Loaders运行后台程序

    安卓的用户界面线程(user interface thread) 1.1 主线程 安卓修改用户界面并从一个单一用户界面线程中处理输入事件,这个线程也被称作主线程(main thread) Androi ...

  5. Swift - 让程序挂起后,能在后台继续运行任务

    1,程序的挂起和退出 由于iOS设备资源有限.当用户点击了home键,或者另一个应用程序启动了.那么原先那个程序便进入后台被挂起,不是退出,只是停止执行代码,同时它的内存被锁定.当应用程序恢复时,它会 ...

  6. 程序自启动及后台持续运行的研究(voip和GPS方式)

    1,voip方式    1)首先要修改应用的plist配置,添加下面的设置:        Application does not run in background: NO        Requ ...

  7. 让java程序在后台一直执行(例如putty关闭后后台程序继续运行)

    如果在终端中执行java -jar xxx.jar&命令,当终端关闭时,xxx.jar也会同时结束运行,但是如果执行nohup java -jar xxx.jar&命令,则程序会在后台 ...

  8. ios后台程序持续运行方法

    iOS系统的资源是有限的,应用程序在前台和在后台的状态是不一样的.在后台时,程序会受到系统的很多限制,这样可以提高电池的使用和用户体验.但是有很多社交类的软件,如果因为它在后台就不能刷新到新的数据的话 ...

  9. 【iOS】swift 让程序挂起后,能在后台继续运行任务

    1,程序的挂起和退出 由于iOS设备资源有限.当用户点击了home键,或者另一个应用程序启动了.那么原先那个程序便进入后台被挂起,不是退出,只是停止执行代码,同时它的内存被锁定.当应用程序恢复时,它会 ...

随机推荐

  1. input和raw_input

    Python2.X使用raw_input() Python3.X废弃了raw_input()函数,使用input()函数替代它 code: data=input("please input ...

  2. IE兼容性小结(IE7及以上版本)

    缘由 在急速的互联网时代,原本ie系列的低版本浏览器就该淘汰了.可偏偏还是有很多xp系统ie7.8浏览器,这有让网站甚是苦逼.练就了一套新技术,原本以为能大展身手,可却在ie阵容中认怂了,不得不规规矩 ...

  3. MySQL--REPALCE INTO操作

    REPLACE INTO语法是MySQL数据库独特的扩展语法,可以提供“不存在即插入,存在即更新”的操作,MySQL官方文档解析其算法为: 1.尝试进行INSER 操作 2.如果INSERT 失败,则 ...

  4. 基于Windows 机器学习(Machine Learning)的图像分类(Image classification)实现

    今天看到一篇文章  Google’s Image Classification Model is now Free to Learn  说是狗狗的机器学习速成课程(Machine Learning C ...

  5. 剑指offer编程题Java实现——面试题7用两个栈实现队列

    题目:用两个栈实现一个队列.队列的声明如下:请实现他的两个函数appendTail和deleteHead, 分别完成在队列尾部插入节点和在队列头部删除节点的功能. package Solution; ...

  6. AJPFX:外汇的杠杆保证金是什么

    外汇杠杆和保证金两者有着密切的关系.杠杆越大,交易时所用的保证金就越少. 杠杆即为保证金可以缩小的倍数.例如在没有杠杆的情况下,做一手即10万的欧元兑美元货币对合约(现在价格是1.05821),您所需 ...

  7. 分组,命名分组,url的命名和反向解析

    1.位置分组 匹配到参数,按照位置参数的方式传递给视图函数 视图函数需要定义形参接收变量 1.写在url里面的: # 删除 url(r'^del_class/(\d+)',views.del_clas ...

  8. Python之分支结构

    if lengeh >= 100: #每个条件后面要使用冒号 if lengeh>=10000: ") elif lengeh>=1000: ') else: pass e ...

  9. Postgres 的 JSON / JSONB 类型

    从 MySQL 5.7.8 开始,MySQL 支持原生的 JSON 数据类型. 一.介绍 json是对输入的完整拷贝,使用时再去解析,所以它会保留输入的空格,重复键以及顺序等.而jsonb是解析输入后 ...

  10. 启动HDFS之后一直处于安全模式org.apache.hadoop.hdfs.server.namenode.SafeModeException: Log not rolled. Name node is in safe mode.

    一.现象 三台机器 crxy99,crxy98,crxy97(crxy99是NameNode+DataNode,crxy98和crxy97是DataNode) 按正常命令启动HDFS之后,HDFS一直 ...