淺談iOS的多任務機制

來源:互聯網
上載者:User

對320的程式持久化高度興趣,於是首先研究了下iOS的多任務機制.對於大多數的應用,如果不是特別需要,用最新的SDK編譯出來的程式本身就是支援多任務的—按home鍵程式進入後台運行(但是注意此時的程式並不是會運行,只是進入後台狀態便於其再次進入活動狀態,這一點同我們概念中應該有的多任務有區別).而對於一些應用,是需充分運用iOS多任務的特性,如遊戲應用和需要網路連接的應用等等.

(轉載請註明 www.cnblogs.com/scorpiozj)

多任務機制是蘋果在iOS4中引進的,我們首先建立一個工程,查看下appdelegate檔案中的內容:

App cycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

// Override point for customization after application launch.

// Set the view controller as the window's root view controller and display.

return YES;
}
- (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 {
/*
Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
*/
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
/*
Called as part of 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.
See also applicationDidEnterBackground:.
*/
}

以上的函數就是ios4中引入的用於支援多任務啟動並執行函數.從函數名我們就可以猜測到程式可能的一些狀態:background,active.
圖1就是程式的生命週期圖.

圖1

我們來看一下:程式第一次啟動,從Not
Running狀態進入active階段,程式會調用兩個函數:

  1. didFinishLaunchingWithOptions:和
  2. applicationDidBecomeActive:

此時點擊home鍵,程式就會進入後台直至進入掛起狀態,程式會依次調用下述函數:

  1. applicationWillResignActive:
  2. applicationDidEnterBackground:

這時雙擊home鍵再進入該程式時,程式會依次調用:

  1. applicationWillEnterForeground:
  2. applicationDidBecomeActive:

注意到Background包含running和suspend兩種狀態.這裡的running不是真正意義上的程式運行,而是指的applicationDidEnterBackground:運行部分,通常情況下,系統給此函數執行的時間不會太多,並且執行完後程式就在後台掛起.這就是iOS多任務的絕大部分情形,但是等一下,就三種情況蘋果是允許你在後台啟動並執行                                       

  • 音樂
  • 位置
  • VoIP

你可以在程式中的plist檔案中進行設定,2:

                                                         

圖2

可以這麼說,除了這三種服務允許以我們通常認為的多任務機制在後台運行,其他的程式的多任務,就是上文所指的程式狀態.這裡不討論為啥蘋果要這樣做,只是稍微說明一下,在這種情況下,至少是在表面上實現了多任務:比如說我玩了憤怒的小鳥又想看下天氣,然後又想玩小鳥了,這時切換就比較容易,因為程式這時是在後台掛起,還是在記憶體中運行著的,這樣再啟動時就會比較快.

程式調用相應函數的時候,系統會發送相應的Notification,這時app就應該適時的儲存app狀態或是讀取app的曆史狀態,這樣才能更好的呈現使用者體驗.

之前說過,程式進入背景時間很短,有時候app要做的操作還沒來得及進行.這時,我們可以使用beginBackgroundTaskWithExpirationHandler:來處理耗時可能比較長的操作.

總結一下:

ios所謂的多任務並不是我們通常以為的多任務,ios只允許三種服務在後台運行;其他的只是方便多個app之間的切換.

(至於如何?多任務,比如進入背景程式應該優先進行哪些操作的細節,會在以後結合本人的實際加以說明.)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.