博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
GCD之后台程序运行
阅读量:6557 次
发布时间:2019-06-24

本文共 2438 字,大约阅读时间需要 8 分钟。

点击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

 

转载地址:http://kvtco.baihongyu.com/

你可能感兴趣的文章
Tensflow的targmax函数
查看>>
疑似checkpoint堵塞数据库连接
查看>>
Node.js中针对中文的查找和替换无效的解决方法
查看>>
理解指针的关键
查看>>
如何查看Ubuntu下已安装包版本号
查看>>
MS SQL巡检系列——检查重复索引
查看>>
我的那些年(2)~我毕业了
查看>>
VS2017 配置ImageMagick
查看>>
scrapy 直接在编辑器运行
查看>>
微信小程序Tab选项卡切换大集合
查看>>
Hive任务优化--控制hive任务中的map数和reduce数
查看>>
[摄影]上海往事
查看>>
『原创』c#实现文件加密、解密及文件拖拽至程序图标直接打开
查看>>
【Leetcode】Search in Rotated Sorted Array
查看>>
redis3.0.0 集群安装详细步骤
查看>>
WCF 之 初识WCF
查看>>
如何在Linux命令行中创建以及展示演示稿
查看>>
FutureTask——另一种闭锁的实现
查看>>
js-ES6学习笔记-Proxy
查看>>
Android和MVC
查看>>