IOS 4.0 以上版本 home键退出 后台执行代码
生活随笔
收集整理的這篇文章主要介紹了
IOS 4.0 以上版本 home键退出 后台执行代码
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
今天調(diào)查了下IOS 4.0 支持的多任務(wù)的事宜,系統(tǒng)是4.2, 初步結(jié)果如下:
Ios 4.0 多任務(wù)不是傳統(tǒng)意義上的多任務(wù)。只是把程序的狀態(tài)保存起來,程序掛起。因?yàn)锳pple還沒準(zhǔn)備好多任務(wù)同時(shí)運(yùn)行,
主要是因?yàn)閎attery和memory這兩個(gè)問題還沒有解決。
現(xiàn)在IOS 4多任務(wù)支持的類型(官網(wǎng)):
<!--[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, 既當(dāng)用戶掛起程序時(shí),如果還有task沒完成,可以把改task完成。
但這個(gè)是有限制的,時(shí)間的限制,就是說你的后臺(tái)程序不能執(zhí)行超過某個(gè)時(shí)間。
我剛才打log看了,系統(tǒng)返回500s,既是8分鐘,8分鐘如果還沒執(zhí)行完,就會(huì)自動(dòng)把我們程序結(jié)束。
代碼如下 #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];
}
}
Ios 4.0 多任務(wù)不是傳統(tǒng)意義上的多任務(wù)。只是把程序的狀態(tài)保存起來,程序掛起。因?yàn)锳pple還沒準(zhǔn)備好多任務(wù)同時(shí)運(yùn)行,
主要是因?yàn)閎attery和memory這兩個(gè)問題還沒有解決。
現(xiàn)在IOS 4多任務(wù)支持的類型(官網(wǎng)):
<!--[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, 既當(dāng)用戶掛起程序時(shí),如果還有task沒完成,可以把改task完成。
但這個(gè)是有限制的,時(shí)間的限制,就是說你的后臺(tái)程序不能執(zhí)行超過某個(gè)時(shí)間。
我剛才打log看了,系統(tǒng)返回500s,既是8分鐘,8分鐘如果還沒執(zhí)行完,就會(huì)自動(dòng)把我們程序結(jié)束。
代碼如下 #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];
}
}
總結(jié)
以上是生活随笔為你收集整理的IOS 4.0 以上版本 home键退出 后台执行代码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 简单点儿、简单点儿、再简单点儿,其实世界
- 下一篇: 自用开源/免费软件收集