ios-multitasking-应用转入后台时,如何继续后台运行任务
2014-04-13 23:18
411 查看
启动后台运行任务时,调用UIApplication的实例方法beginBackgroundTaskWithExpirationHandler:
任务完成后,调用UIApplication实例方法endBackgroundTask:
运行结果:
2012-09-22 15:10:57.618 test[2253:f803] Background time remaining = 599.00 seconds
2012-09-22 15:10:58.618 test[2253:f803] Background time remaining = 598.00 seconds
2012-09-22 15:10:59.617 test[2253:f803] Background time remaining = 597.00 seconds
2012-09-22 15:11:00.618 test[2253:f803] Background time remaining = 596.00 seconds
2012-09-22 15:11:01.618 test[2253:f803] Background time remaining = 595.00 seconds
2012-09-22 15:11:02.617 test[2253:f803] Background time remaining = 594.00 seconds
2012-09-22 15:11:03.618 test[2253:f803] Background time remaining = 593.00 seconds
任务完成后,调用UIApplication实例方法endBackgroundTask:
//AppDelegate.h 02 03 #import <UIKit/UIKit.h> 04 05 @interface AppDelegate : UIResponder <UIApplicationDelegate> 06 07 @property (strong, nonatomic) UIWindow *window; 08 09 @property (nonatomic, unsafe_unretained) UIBackgroundTaskIdentifier backgroundIdentifier; 10 11 @property (nonatomic, strong) NSTimer *myTimer; 12 13 <a href="http://my.oschina.net/u/567204" class="referer" target="_blank">@end</a> view source print? 01 // AppDelegate.m 02 // test 03 // 04 05 #import "AppDelegate.h" 06 07 @implementation AppDelegate 08 09 @synthesize window; 10 @synthesize backgroundIdentifier; 11 @synthesize myTimer; 12 13 - (BOOL)isMultitaskingSupported 14 { 15 BOOL result = NO; 16 if ([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)]) { 17 result = [[UIDevice currentDevice] isMultitaskingSupported]; 18 } 19 return result; 20 } 21 22 - (void)timerMethod:(NSTimer *)paramSender{ 23 //获取后台任务可执行时间,单位秒,若应用未能在此时间内完成任务,则应用将被终止 24 NSTimeInterval backgroundTimeRemaining = [[UIApplication sharedApplication] backgroundTimeRemaining]; 25 26 //应用处于前台时,backgroundTimeRemaining值weiDBL_MAX 27 if (backgroundTimeRemaining == DBL_MAX) { 28 NSLog(@"Background time remaining = Undetermined"); 29 } else { 30 NSLog(@"Background time remaining = %.02f seconds", backgroundTimeRemaining); 31 } 32 } 33 34 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 35 { 36 if ([self isMultitaskingSupported]) { 37 NSLog(@"Multitasking is supported."); 38 } else { 39 NSLog(@"Multitasking is not supported."); 40 } 41 42 return YES; 43 } 44 45 - (void)applicationWillResignActive:(UIApplication *)application 46 { 47 // 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. 48 // 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. 49 } 50 51 - (void)applicationDidEnterBackground:(UIApplication *)application 52 { 53 if ([self isMultitaskingSupported] == NO){ 54 return; } 55 self.myTimer = 56 [NSTimer scheduledTimerWithTimeInterval:1.0f 57 target:self 58 selector:@selector(timerMethod:) userInfo:nil 59 repeats:YES]; 60 self.backgroundIdentifier = [application beginBackgroundTaskWithExpirationHandler:^(void) { 61 [self endBackgroundTask]; 62 }]; 63 } 64 65 - (void) endBackgroundTask{ 66 dispatch_queue_t mainQueue = dispatch_get_main_queue(); 67 __weak AppDelegate *weakSelf = self; 68 dispatch_async(mainQueue, ^(void) { 69 AppDelegate *strongSelf = weakSelf; 70 if (strongSelf != nil){ 71 [strongSelf.myTimer invalidate]; 72 [[UIApplication sharedApplication] endBackgroundTask:self.backgroundIdentifier]; 73 strongSelf.backgroundIdentifier = UIBackgroundTaskInvalid; 74 } 75 }); 76 } 77 78 - (void)applicationWillEnterForeground:(UIApplication *)application 79 { 80 // 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. 81 } 82 83 - (void)applicationDidBecomeActive:(UIApplication *)application 84 { 85 // 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. 86 } 87 88 - (void)applicationWillTerminate:(UIApplication *)application 89 { 90 // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 91 } 92 93 <a href="http://my.oschina.net/u/567204" class="referer" target="_blank">@end</a>
运行结果:
2012-09-22 15:10:57.618 test[2253:f803] Background time remaining = 599.00 seconds
2012-09-22 15:10:58.618 test[2253:f803] Background time remaining = 598.00 seconds
2012-09-22 15:10:59.617 test[2253:f803] Background time remaining = 597.00 seconds
2012-09-22 15:11:00.618 test[2253:f803] Background time remaining = 596.00 seconds
2012-09-22 15:11:01.618 test[2253:f803] Background time remaining = 595.00 seconds
2012-09-22 15:11:02.617 test[2253:f803] Background time remaining = 594.00 seconds
2012-09-22 15:11:03.618 test[2253:f803] Background time remaining = 593.00 seconds
相关文章推荐
- 【iOS】swift 让程序挂起后,能在后台继续运行任务
- iOS 程序退到后台,如何继续执行任务
- 【iOS】swift 让程序挂起后,能在后台继续运行任务
- iOS 应用进入后台后,如何短暂的执行一个任务
- 如何在Ubuntu手机上让自己的应用退到后台还继续运行
- ios程序后台继续运行
- IOS实现Voip应用后台运行需要的几个配置项
- IOS实现Voip应用后台运行需要的几个配置项
- Swift - 让程序挂起后,能在后台继续运行任务
- 如何通过Linux bash在后台运行任务
- Parse教程——如何简单地使用Parse在iOS应用创建后台服务
- iOS GCD创建标准定时器,进入后台后任可继续定时,无需开启后台任务
- iOS 7后台多任务(multitasking)机制
- 【已解决】Android 如何让应用在后台运行
- 玩玩Linux云主机-linux后台运行和关闭、查看后台任务,关闭终端后继续执行
- iOS后台任务继续Long-Running Task
- iOS的后台运行和多任务处理
- iOS应用在后台长久运行
- iOS 如何让程序在后台运行
- IOS程序后台运行及多任务