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




相关文章
|
2天前
|
Linux Shell vr&ar
进程从代码到二进制到运行时的过程
【4月更文挑战第18天】Linux系统中二进制程序的格式,介绍了ELF(Executable and Linkable Format)格式。
|
2月前
|
自然语言处理 Java 编译器
程序是如何运行的(一)
程序是如何运行的(一)
|
3月前
|
调度
程序后台执行与前台执行速度出现差异
程序后台执行与前台执行速度出现差异
29 0
|
8月前
如何停止一个正在运行的线程
如何停止一个正在运行的线程
48 1
|
11月前
|
监控 Unix Linux
高效管理 Linux 进程:如何后台执行程序、查看进程、终止任务
高效管理 Linux 进程:如何后台执行程序、查看进程、终止任务
|
11月前
|
消息中间件 JavaScript 小程序
如何停止一个正在运行的线程?我一脸蒙蔽。。。 上
如何停止一个正在运行的线程?我一脸蒙蔽。。。 上
|
11月前
|
Java
如何停止一个正在运行的线程?我一脸蒙蔽。。。 下
如何停止一个正在运行的线程?我一脸蒙蔽。。。 下
|
Linux
Linux系统查询指定路径下的进程,根据进程id号杀进程方法,进程卡死解决方法实例演示
Linux系统查询指定路径下的进程,根据进程id号杀进程方法,进程卡死解决方法实例演示
201 0
Linux系统查询指定路径下的进程,根据进程id号杀进程方法,进程卡死解决方法实例演示
|
前端开发 Java 应用服务中间件
|
监控 Windows
艾伟:.Net 下跟踪线程挂起和程序死循环
.Net 下调试跟踪线程挂起和程序死循环   作者:Eaglet      .Net 下的程序调试相对C/C++要简单很多,少了那些令人头疼的指针越界的问题。不过当你的程序遇到如下问题时,依然非常棘手:      1. 进程异常终止。
1001 0