Android Process Lifecycle(行程生命週期)

1 篇文章 / 0 new
author
Android Process Lifecycle(行程生命週期)
Android系統記憶體不足時, 就需要把舊的或不需要用的應用程式移除. 如同之前的Activity生命週期所介紹, 這個移除的決定是由應用程式所處的狀態來判斷. 一般來說,當需要移除應用程式時, 系統將會做排序, 然後從最不重要的開始移除, 以下是移除時的考量順序:

1. 最早被移除的是 Empty Process(空行程):
Empty process 是指那些沒有跟Activity綁定, 也沒有跟任何的應用程式元件(比如Service或IntentReceiver)綁定在一起的process, 這些空行程一定是最早被系統考慮移除的.


2. 第2順位考慮被移除的是 Background Activity.
Background Activity指這個activity是無法被使用者看到的的情況, 表示Activity已處於stop的狀態, 系統移除這些 Activity 是安全的. 通常有多個 Background Activity同時運行, 這些Activity被存放在一個 LRU (least recent used) list中, 系統可以根據 LRU list 判斷哪些 Activity可以被移除, 哪一個應該是最先被移除的.

3. 第3順位被移除的是 Service Process.
在 Android 應用程式裡, 有一種沒有 UI 的類別(android.app.Service), 稱之為 Service. Service Process 通常是由startService()方式啟動. 簡單來說,Service 屬於 background(背景)程序, 透過背景程序, 我們可以製作一些不需要 UI 的功能, 例如: 在背景撥放音樂, 上傳或下載文件等. 系統通常會保護它, 除非真的沒有記憶體可用.

4. 接著輪到 Visible Activity / Visible Process:
Visible Process是一個可被Visible的, 但是沒有顯示在最上端 (onPause被使用時). 舉例來說, 當一個新的對話框Activity出現時, 原來的Activity仍然visible, 仍然被系統認為是重要的, 通常不會被移除. 但若不得不移除時, 由於屬於 Paused狀態, 相對來說, 它已經處於一個比較安全的位置.

5. 最後被移除的是 Foreground Activity:
Foreground是一個在螢幕最上端與使用者做互動的Activity, 它的優先權最高. 原則上會是最後一個被移除的程式, 除非這個Activity所需要的記憶體大小已經超出系統所能給的. 系統之所以會移除這些程序, 是為了不讓使用者介面停止回應.

由於Android應用程式的lifecycle並不是由程式本身直接控制的, 而是由系統平台進行管理. 所以對於開發者而言, 許要了解不同元件 Activity, Serivce 和 IntentReceiveer的Lifecycle. 要切記: 如果元件使用不當, 雖然正在進行重要的Process, 仍有可能被系統主動移除.


There are

five levels in the hierarchy. The following list presents them in order of importance:
1.A foreground process
2.A visible process
3.A service process
4.A background process
5.An empty process
以上這五種進程,由高至低,排序了其進程在系統裡的優先權。

底下詳細說明這5種進程。
什麼是前景進程 (foreground process)?

  • It is running an activity that the user is interacting with (the Activity object's <code><a href="<a href="http://developer.android.com/reference/android/app/Activity.html#onResume%28%29">onResume">http://developer.android.com/reference/android/app/Activity.html#onResum...</a>()</a> method has been called). - Activity正在被使用者使用的當下(onResume()事件發生時)
  • It hosts a service that's bound to the activity that the user is interacting with. - 一個activity有互動的Service
  • It has a <a href="<a href="http://developer.android.com/reference/android/app/Service.html">Service</a>">http://developer.android.com/reference/android/app/Service.html">Service... object that's executing one of its lifecycle callbacks (<code><a href="<a href="http://developer.android.com/reference/android/app/Service.html#onCreate%28%29">onCreate">http://developer.android.com/reference/android/app/Service.html#onCreate...</a>()</a>, <code><a href="<a href="http://developer.android.com/reference/android/app/Service.html#onStart%28android.content.Intent,%20int%29">onStart">http://developer.android.com/reference/android/app/Service.html#onStart%...</a>()</a>, or <code><a href="<a href="http://developer.android.com/reference/android/app/Service.html#onDestroy%28%29">onDestroy">http://developer.android.com/reference/android/app/Service.html#onDestro...</a>()</a>). - 有一個Service物件,而且這個Service物件還執行callback回呼函式(像Service裡的onCreate()、onStart()或onDestroy()之類的)
  • It has a <a href="<a href="http://developer.android.com/reference/android/content/BroadcastReceiver.html">BroadcastReceiver</a>">http://developer.android.com/reference/android/content/BroadcastReceiver... object that's executing its <code><a href="<a href="http://developer.android.com/reference/android/content/BroadcastReceiver.html#onReceive%28android.content.Context,%20android.content.Intent%29">onReceive">http://developer.android.com/reference/android/content/BroadcastReceiver...</a>()</a> method. - 有一個廣播監聽物件,而且該廣播還執行了onReceive這個callback回呼函式。
Only a few foreground processes will exist at any given time. They are killed only as a last resort.
因為在同樣的時間裡,只會有少數的前景進程存在,所以它們被排到最最最最最後面,才有機會被系統砍殺。





什麼是可用進程 (visible process)?

A visible process is one that

doesn't have any foreground components, but still can affect what the user sees on screen.
可用進程沒會有任何的前景元件執行(像Activity),但是呢,它卻仍會影響使用者所看的螢幕畫面。

  • It hosts an activity that is not in the foreground, but is still visible to the user (its <code><a href="<a href="http://developer.android.com/reference/android/app/Activity.html#onPause%28%29">onPause">http://developer.android.com/reference/android/app/Activity.html#onPause...</a>()</a> method has been called). This may occur, for example, if the foreground activity is a dialog that allows the previous activity to be seen behind it. - 像是我們叫出了一個畫面裡的訊息對話視窗,此時原程式的onPause()被呼叫,我們稱此時該進程為可用進程
  • It hosts a service that's bound to a visible activity. - 一個綁到可用activity中的Service

A visible process is considered extremely important and will not be killed unless doing so is required to keep all foreground processes running.
可用進程也是非常重要的,系統會盡量的讓所有這些前景進程保持運作中,除非非不得己,才會砍掉。


什麼是Service進程 (service process)?
  Although service processes are not directly tied to anything the user sees, they are generally doing things that

the user cares about (such as playing an mp3 in the background or downloading data on the network), so the system keeps them running unless there's not enough memory to retain them along with all foreground and visible processes.
雖然Service進程不是對使用者所觀看到的畫面產生直接影響的元件,但是它們一般都被拿來做一些使用者在乎的事,像是在背景播mp3啦、從網路下載檔案啦…,所以,系統在記憶體不足以前,也會如同前景進程和可用進程一樣努力的保持著它的作業。


什麼是背景進程 (background process)?
A background process is

one holding an activity that's not currently visible to the user (the Activity object's <code><a href="<a href="http://developer.android.com/reference/android/app/Activity.html#onStop%28%29"><em">http://developer.android.com/reference/android/app/Activity.html#onStop%...</a> class="diigoHighlight a id_accb22b9df5a36ac05c4e3f4898ee1a3 type_0 yellow">onStop()</em></a> method has been called).
背景進程是一個使用者沒有在使用狀態的activity(此時Activity的onStop()會被系統呼叫)
因為通常系統如果呼叫到程式的onStop時,幾乎可以說該應用程式已經不是使用者當下想執行了。所以Android可以很大膽的先處理掉這種進程,這麼做,一點都不影響使用者的使用體驗。


什麼是空進程 (empty process)?
An empty process is one that

doesn't hold any active application components. The only reason to keep such a process around is as a cache to improve startup time the next time a component needs to run in it. 
空進程是一個沒有執行任何啟動中的應用程式元件的進程。它存在唯一的理由就是保存cache,好讓下一次啟動元件時需要的元件,可以被快速的被找到和執行。
The system often kills these processes in order to balance overall system resources between process caches and the underlying kernel caches.
這種進程最容易被砍殺,為了讓系統保持良好的循環平衡。


from
http://tyroandroid.blogspot.com/2009/07/android-process-lifecycle.html
http://lp43.blogspot.com/2010/12/process-and-lifecycles.html
Free Web Hosting