启动后台运行任务时,调用UIApplication的实例方法beginBackgroundTaskWithExpirationHandler:
任务完成后,调用UIApplication实例方法endBackgroundTask:
03
#import <UIKit/UIKit.h>
05
@interface AppDelegate : UIResponder <UIApplicationDelegate>
07
@property (strong, nonatomic) UIWindow *window;
09
@property (nonatomic, unsafe_unretained) UIBackgroundTaskIdentifier backgroundIdentifier;
11
@property (nonatomic, strong) NSTimer *myTimer;
13
<a href=
"http://my.oschina.net/u/567204"
class
=
"referer"
target=
"_blank"
>@end</a>
05
#import "AppDelegate.h"
07
@implementation AppDelegate
10
@synthesize backgroundIdentifier;
13
- (
BOOL
)isMultitaskingSupported
16
if
([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)]) {
17
result = [[UIDevice currentDevice] isMultitaskingSupported];
22
- (
void
)timerMethod:(NSTimer *)paramSender{
24
NSTimeInterval backgroundTimeRemaining = [[UIApplication sharedApplication] backgroundTimeRemaining];
27
if
(backgroundTimeRemaining == DBL_MAX) {
28
NSLog(@
"Background time remaining = Undetermined"
);
30
NSLog(@
"Background time remaining = %.02f seconds"
, backgroundTimeRemaining);
34
- (
BOOL
)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
36
if
([self isMultitaskingSupported]) {
37
NSLog(@
"Multitasking is supported."
);
39
NSLog(@
"Multitasking is not supported."
);
45
- (
void
)applicationWillResignActive:(UIApplication *)application
51
- (
void
)applicationDidEnterBackground:(UIApplication *)application
53
if
([self isMultitaskingSupported] == NO){
56
[NSTimer scheduledTimerWithTimeInterval:1.0f
58
selector:@selector(timerMethod:) userInfo:nil
60
self.backgroundIdentifier = [application beginBackgroundTaskWithExpirationHandler:^(
void
) {
61
[self endBackgroundTask];
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;
78
- (
void
)applicationWillEnterForeground:(UIApplication *)application
83
- (
void
)applicationDidBecomeActive:(UIApplication *)application
88
- (
void
)applicationWillTerminate:(UIApplication *)application
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
转载自:http://my.oschina.net/notting/blog/79855