Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 資訊

Android 中的 Context 詳解

編輯:Android 資訊

Context 可能是 Android 應用中最常用的元素,也可能是最容易被誤用的。

Context 物件如此常見且被四處傳遞,很容易產生並非你所預期的情形。載入資源、啟動新 Activity、取得系統服務、取得內部檔案路徑以及建立 View(其實遠不止這些)全都需要 Context 物件來完成。我(原文作者)想做的,是給大家提供一些 Context 運作方式的見解,以及在應用中更有效使用 Context 的技巧。

Context 的類型

並非所有 Context 實例都等價。根據 Android 應用元件的不同,你存取的 Context 會有些微差別。

  • Application - 是一個運行於應用進程中的單例。在 Activity 或 Service 中,可透過 getApplication() 函式取得;在任何繼承自 Context 的物件中,則可透過 getApplicationContext() 方法取得。無論用何種方法、在哪裡取得,同一個進程內你總是拿到同一個實例。
  • Activity/Service - 繼承自 ContextWrapper,實作了與 Context 相同的 API,但會把這些方法代理呼叫到內部隱藏的 Context 實例(即基礎 Context)。每當系統建立新的 Activity 或 Service 實例時,也會建立一個新的 ContextImpl 實例來承擔所有繁重工作。每個 Activity 和 Service 及其對應的基礎 Context,對每個實例來說都是唯一的。
  • BroadcastReceiver - 它本身不是 Context,內部也沒有 Context,但每當一個新廣播到達時,框架都會傳遞一個 Context 物件到 onReceive()。這個 Context 是一個 ReceiverRestrictedContext 實例,有兩個主要函式被禁用:registerReceiver() 和 bindService(),這兩個函式在 BroadcastReceiver.onReceive() 中不允許呼叫。每次 Receiver 處理廣播時,傳入的 Context 都是新實例。
  • ContentProvider - 它本身也不是 Context,但可透過 getContext() 函式給你一個 Context 物件。若 ContentProvider 是在呼叫者的本地(例如同一個應用進程),getContext() 會回傳 Application 單例;然而,若呼叫的 ContentProvider 在不同進程,則會回傳一個代表該 Provider 所運行套件的新建立實例。

保存引用

第一個要解決的問題是:在一個物件或類別內部保存 Context 引用,而它的生命週期卻超過了保存引用物件本身的生命週期。例如,建立一個自定義單例,它需要 Context 來載入資源或取得 ContentProvider,於是保存了一個指向當前 Activity 或 Service 的引用。

糟糕的單例

public class CustomManager { private static CustomManager sInstance; public static CustomManager getInstance(Context context) { if (sInstance == null) { sInstance = new CustomManager(context); } return sInstance; } private Context mContext; private CustomManager(Context context) { mContext = context; } }

問題在於,我們不知道這個 Context 從哪裡來,而保存一個最終指向 Activity 或 Service 的引用並不安全。這是因為單例在類別內部維持一個唯一的靜態引用,這意味著我們的物件及它所引用的所有其他物件,將永遠無法被垃圾回收。假如這個 Context 是一個 Activity,我們就會保存與該 Activity 相關的所有 View 及其他大型物件,從而導致內存洩露。

為了解決這個問題,我們修改單例,讓它永遠只保存 Application Context:

改善的單例:

public class CustomManager { private static CustomManager sInstance; public static CustomManager getInstance(Context context) { if (sInstance == null) { //Always pass in the Application Context sInstance = new CustomManager(context.getApplicationContext()); } return sInstance; } private Context mContext; private CustomManager(Context context) { mContext = context; } }

現在這個例子中,我們的 Context 來自哪裡都不重要,因為保存引用是安全的。Application Context 本身就是單例,所以再建立一個 static 引用不會造成任何內存洩露。另一個很好的例子是:在背景執行緒或一個等待的 Handler 中保存 Context 引用時,也可以用這樣的方法。

那為什麼我們不能總是引用 Application Context 呢?正如前面所說,引用 Application Context 永遠不用擔心內存洩露。答案就像我在開頭介紹中說的——因為不同 Context 並不等價。

Context 的能力

Context 能做的通用操作,取決於這個 Context 最初來源於哪裡。下表列出應用中常見會收到的 Context 物件,以及對應每種情況它能用於哪些地方:

Application Activity Service ContentProvider BroadcastReceiver 顯示 Dialog NO YES NO NO NO 啟動 Activity NO1 YES NO1 NO1 NO1 Layout Inflation NO2 YES NO2 NO2 NO2 啟動 Service YES YES YES YES YES 綁定到 Service YES YES YES YES NO 發送 Broadcast YES YES YES YES YES 註冊 BroadcastReceiver YES YES YES YES NO3 加載 Resource YES YES YES YES YES

注:

  1. NO1 表示 Application Context 的確可以啟動一個 Activity,但它需要建立一個新的 task。這可能滿足某些特定需求,但會在你的應用中建立一個不標準的回退堆疊(back stack),這通常不推薦或不是好的實踐。
  2. NO2 表示這是非法的,但填充(inflation)的確可以完成,只是它會使用系統預設的主題(theme),而非你 App 定義的主題。
  3. NO3 在 Android 4.2 以上,若 Receiver 為 null(用於取得 sticky broadcast 當前值),這是允許的。

使用者介面 UI

從前面的表格可以看到,Application Context 有許多不適合執行的功能,而這些功能都與 UI 相關。實際上,只有 Activity 能處理所有與 UI 相關的任務,其他類別的 Context 實例功能都差不多。

幸好,應用中這三種操作基本上都不需要在 Activity 範圍外進行,這很可能是 Android 框架刻意如此設計的。嘗試用 Application Context 顯示 Dialog,或用 Application Context 啟動 Activity,系統會拋出異常讓應用崩潰,強烈告知你某些地方出了問題。

一個並不明顯的問題是填充布局(inflating layout)。如果你讀過我(原文作者)上一篇關於 Layout inflation 的文章,就知道它是一個相當神秘的過程,伴隨一些隱藏行為,而使用正確的 Context 關乎其中一個行為。當你用 Application Context 來 inflate 一個布局時,框架並不會報錯,而是回傳一個使用系統預設主題建立的完美 View 給你,完全沒有考慮你 App 自定義的主題和樣式。這是因為 Activity 是唯一綁定了 manifest 檔中所定義主題的 Context,其他 Context 實例都會用系統預設主題來 inflate 你的 View,導致顯示結果並非你所期望的。

規則的路口

可能有讀者已經得出兩個規則互相矛盾的結論。在某些應用設計中,我們可能既必須長期保存一個引用,又為了完成與 UI 相關的工作而必須保存一個 Activity。若出現這種情況,我會強烈建議你重新考慮設計,它將會是一個很好的「反框架」教材。

經驗法則

絕大多數情況下,使用你所在元件內部能直接取得的 Context。只要這個引用沒有超過該元件的生命週期,你就可以安全保存它。一旦你要保存的 Context 引用超過了 Activity 或 Service 的生命週期範圍(哪怕是暫時的),就需要將引用轉換為 Application Context。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved