#import "AppDelegate.h"
#import "ViewController.h"
@implementation AppDelegate
- (void)dealloc
{
[_window release];
[_viewController release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
- (void)timerAction:(NSTimer *)timer
{
count ++;
NSLog(@"....%i", count);
if (count % 500 == 0){
// 开启一个新的任务
UIApplication *appliction = [UIApplication sharedApplication];
//先关闭之前,再开启
[appliction endBackgroundTask:taskId];
taskId = [appliction beginBackgroundTaskWithExpirationHandler:nil];
}
if (count == 700){
NSLog(@"500500llgalrgjalerjgl");
}
}
- (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
{
// 后台任务 开启, 进入后台运行,(自定义一个任务)默认只有600s
taskId = [application beginBackgroundTaskWithExpirationHandler:^{
//结束任务
[application endBackgroundTask:taskId];
}];
//进入后台时候,开启一个定时器 监听时间
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
}
- (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.
}
- (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
后台程序
最新推荐文章于 2024-08-16 08:57:41 发布