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键,或者另一个应用程序启动了.那么原先那个程序便进入后台被挂起,不是退出,只是停止执行代码,同时它的内存被锁定.当应用程序恢复时,它会 ...
 
随机推荐
- ASP.NET Web API 框架研究 Self Host模式下的消息处理管道
			
Self Host模式下的ASP.NET Web API与WCF非常相似,都可以寄宿在任意类型的托管应用程序中,宿主可以是Windows Form .WPF.控制台应用以及Windows Servic ...
 - Delphi实现拍照控件的程序代码
			
完整的delphi拍照控件代码,实现利用摄像头进行拍照的功能.需要TVideoCap控件支持. procedure Tfrm1.Button2Click(Sender: TObject); Var j ...
 - 将Azure计算机视觉添加到Xamarin应用程序简单体验
			
微软Azure提供了大量的AI及机器学习功能,可以通过简单的RestAPI调用. 关于此文中提到的Azure计算机视觉,可查看此链接的详细介绍. 通过微软的服务,只需要几行代码即可使用计算机视觉中的 ...
 - netcore 发布 到 windows server IIS
			
net core 和普通net 发布没有什么不同,只需要在个别地方注意下: 1. 在visual Studio 2017 发布 2. 把发布好的文件copy到服务器上,并新建一个网站,同时要注意选择无 ...
 - wpf 导出Excel
			
private void Button_Click_1(object sender, RoutedEventArgs e) { ExportDataGridSaveAs(true, this.data ...
 - “全栈2019”Java多线程第二十九章:可重入锁与不可重入锁详解
			
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...
 - 说一下acad的bug及问题
			
using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk. ...
 - css经典布局—Sticky footers布局
			
参考:http://www.w3cplus.com/CSS3/css-secrets/sticky-footers.html 效果:将footer固定到底部.文章内容不足满屏时 footer在底部,超 ...
 - 一步步Cobol 400 上手自学入门教程03 - 数据部
			
数据部的作用 程序中涉及到的全部数据(输入.输出.中间)都要在此定义,对它们的属性进行说明.主要描述以下属性: 数据类型(数值/字符)和存储形式(长度) 数据项之间的关系(层次和层号) 文件与记录的关 ...
 - postgresql-日志表
			
pg_log,数据库日志表postgresqllog CREATE TABLE postgres_log ( log_time timestamp(3) with time zone, 日志生成时间 ...