1、应用程序的状态
Not running未运行:程序没启动。
Inactive未激活:程序在前台运行,不过没有接收到事件。在没有事件处理情况下程序通常停留在这个状态。
Active激活:程序在前台运行而且接收到了事件。这也是前台的一个正常的模式。
Backgroud后台:程序在后台而且能执行代码,大多数程序进入这个状态后会在在这个状态上停留一会。时间到之后会进入挂起状态(Suspended)。有的程序经过特殊的请求后可以长期处于Backgroud状态。
Suspended挂起:程序在后台不能执行代码。系统会自动把程序变成这个状态而且不会发出通知。当挂起时,程序还是停留在内存中的,当系统内存低时,系统就把挂起的程序清除掉,为前台程序提供更多的内存。
2、各个程序运行状态时代理的回调
①告诉代理进程启动但还没进入状态保存
- - (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- {
- NSLog(@"①告诉代理进程启动但还没进入状态保存");
- return YES;
- }
②告诉代理启动基本完成程序准备开始运行
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- {
- self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
- NSLog(@"②告诉代理启动基本完成程序准备开始运行");
- // Override point for customization after application launch.
- self.window.backgroundColor = [UIColor whiteColor];
- [self.window makeKeyAndVisible];
- 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.
- NSLog(@"③当应用程序将要入非活动状态执行,在此期间,应用程序不接收消息或事件,比如来电话");
- }
④当应用程序进入活动状态执行
- - (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.
- NSLog(@"④当应用程序进入活动状态执行");
- }
⑤当程序被推送到后台的时候调用。所以要设置后台继续运行,则在这个函数里面设置即可
- - (void)applicationDidEnterBackground:(UIApplication *)application
- {
- // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
- // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
- NSLog(@"⑤当程序被推送到后台的时候调用");
- [application beginBackgroundTaskWithExpirationHandler:^{
- NSLog(@"begin Background Task With Expiration Handler");
- }];
- }
⑥当程序从后台将要重新回到前台时候调用
- - (void)applicationWillEnterForeground:(UIApplication *)application
- {
- // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
- NSLog(@"⑥当程序从后台将要重新回到前台时候调用");
- }
⑦当程序将要退出是被调用,通常是用来保存数据和一些退出前的清理工作。这个需要要设置UIApplicationExitsOnSuspend的键值
- - (void)applicationWillTerminate:(UIApplication *)application
- {
- // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
- NSLog(@"⑦当程序将要退出是被调用");
- }
⑧当程序载入后执行
- - (void)applicationDidFinishLaunching:(UIApplication *)application
- {
- NSLog(@"⑧当程序载入后执行");
- }
程序启动时:
2014-07-01 15:55:14.706 LifeCycle[5845:60b] ①告诉代理进程启动但还没进入状态保存
2014-07-01 15:55:14.708 LifeCycle[5845:60b] ②告诉代理启动基本完成程序准备开始运行
2014-07-01 15:55:14.709 LifeCycle[5845:60b] ④当应用程序进入活动状态执行
按下Home键返回主界面:
2014-07-01 15:56:11.756 LifeCycle[5845:60b] ③当应用程序将要入非活动状态执行
2014-07-01 15:56:11.814 LifeCycle[5845:60b] ⑤当程序被推送到后台的时候调用
再次打开程序:
2014-07-01 15:57:19.200 LifeCycle[5845:60b] ⑥当程序从后台将要重新回到前台时候调用
2014-07-01 15:57:19.201 LifeCycle[5845:60b] ④当应用程序进入活动状态执行
3、基于警告式响应中断
当出现这种中断时,我们需要在- (void)applicationWillResignActive:(UIApplication *)application方法中进行如下操作:
①停止timer 和其他周期性的任务
②停止任何正在运行的请求
③暂停视频的播放
④如果是游戏那就暂停它
⑤减少OpenGL ES的帧率
⑥挂起任何分发的队列和不重要的操作队列(你可以继续处理网络请求或其他时间敏感的后台任务)
当程序回到active状态,我们需要在- (void)applicationDidBecomeActive:(UIApplication *)application方法中重新开始上述任务。不过游戏要回到暂停状态,不能自动开始。