IOS 4.0 以上版本 home键退出 后台执行代码

本文探讨了iOS 4.0系统的多任务处理机制,并非传统意义上的多任务,而是通过保存程序状态实现程序挂起。文章详细介绍了支持的多任务类型,包括背景音频、VoIP、位置更新等,并提供了Task Finishing的具体实现代码。
今天调查了下IOS 4.0 支持的多任务的事宜,系统是4.2, 初步结果如下:



Ios 4.0 多任务不是传统意义上的多任务。只是把程序的状态保存起来,程序挂起。因为Apple还没准备好多任务同时运行,

主要是因为battery和memory这两个问题还没有解决。



现在IOS 4多任务支持的类型(官网):

<!--[if !supportLists]-->§ <!--[endif]-->Background audio

<!--[if !supportLists]-->§ <!--[endif]-->Voice over IP

<!--[if !supportLists]-->§ <!--[endif]-->Background location

<!--[if !supportLists]-->§ <!--[endif]-->Push notifications

<!--[if !supportLists]-->§ <!--[endif]-->Local notifications

<!--[if !supportLists]-->§ <!--[endif]-->Task finishing - If your app is in mid-task when your customer leaves it, the app can now keep running to finish the task.

<!--[if !supportLists]-->§ <!--[endif]-->Fast app switching - All developers should take advantage of fast app switching, which allows users to leave your app and come right back to where they were when they left - no more having to reload the app.



我使用的是Task finishing, 既当用户挂起程序时,如果还有task没完成,可以把改task完成。



但这个是有限制的,时间的限制,就是说你的后台程序不能执行超过某个时间。

我刚才打log看了,系统返回500s,既是8分钟,8分钟如果还没执行完,就会自动把我们程序结束。



代码如下
#pragma mark -

#pragma mark Background Task Handle

- (void)applicationDidEnterBackground:(UIApplication *)application {


// Request permission to run in the background. Provide an

// expiration handler in case the task runs long.

NSAssert(self->bgTask == UIBackgroundTaskInvalid, nil);

self->bgTask = [application beginBackgroundTaskWithExpirationHandler: ^{

// Synchronize the cleanup call on the main thread in case

// the task catully finished at around the same time.

dispatch_async(dispatch_get_main_queue(), ^{

if (UIBackgroundTaskInvalid != self->bgTask) {

[application endBackgroundTask:self->bgTask];

self->bgTask = UIBackgroundTaskInvalid;

}

});

}];

// Start the long-running task and return immediately.

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),

^{

// Do the work assoicated with the task.

for(int i = 0; i < 1000; i++) {

//request network.

NSLog(@"hahah %d, Time Remain = %f", i, [application backgroundTimeRemaining]);

}

// Synchronize the cleanup all on the main thread in case

// the task catully finished at around the same time.

dispatch_async(dispatch_get_main_queue(), ^{


if (UIBackgroundTaskInvalid != self->bgTask) {


[application endBackgroundTask:self->bgTask];

self->bgTask = UIBackgroundTaskInvalid;

}

});

});

}

#pragma mark -

#pragma mark Local Notifications

- (void)scheduleAlarmForDate:(NSDate *)theDate {

UIApplication *app = [UIApplication sharedApplication];

NSArray *oldNotifications = [app scheduledLocalNotifications];

// Clear out the old notification before scheduling a new one.

if (0 < [oldNotifications count]) {

[app cancelAllLocalNotifications];

}

// Create a new notification

UILocalNotification *alarm = [[UILocalNotification alloc] init];

if (alarm) {


alarm.fireDate = theDate;

alarm.timeZone = [NSTimeZone defaultTimeZone];

alarm.repeatInterval = 0;

alarm.soundName = @"ping.caf";//@"default";

alarm.alertBody = [NSString stringWithFormat:@"Time to wake up!Now is\n[%@]",

[NSDate dateWithTimeIntervalSinceNow:10]];

[app scheduleLocalNotification:alarm];

[alarm release];

}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值