天天看點

Android中的Context<1>

    在安卓開發的過程中,我們一開始就會接觸到一個叫上下文的東西,也即Context,有時用this,有時用getApplication,還有的時候會有getContext代替,那麼上下文究竟是什麼的,上下文在Android開發中又起着什麼樣的作用呢,這裡就一起探讨一下。

    Context,中文直譯為“上下文”,SDK中對其說明如下:Interface to global information about an application environment. This is an abstract class whose implementationis provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc

    從上可知以下三點,即:

1、它描述的是一個應用程式環境的資訊,即上下文。

2、該類是一個抽象(abstract class)類,Android提供了該抽象類的具體實作類(後面我們會講到是ContextIml類)。

3、通過它我們可以擷取應用程式的資源和類,也包括一些應用級别操作

    Android中的Context可以了解成場景,場景是什麼什麼意思呢?如果把Android程式的整個運作期間看成一部電影的話,那麼一個Activity或者一個Service就可以稱之為一個場景,一個電影可以包括多個場景。

咱們先看下Context相關的繼承結構:

Android中的Context<1>

      熟悉了Context的繼承關系後,我們接下來分析應用程式在什麼情況需要建立Context對象的?應用程式建立Context執行個體的情況有如下幾種情況:

      1、建立Application 對象時,而且整個App共一個Application對象

      2、建立Service對象時

      3、建立Activity對象時

    是以應用程式App共有的Context數目公式為:

 總Context執行個體個數 = Service個數 +Activity個數 + 1(Application對應的Context執行個體)

相關類介紹:

Context類 路徑: /frameworks/base/core/java/android/content/Context.java

說明:抽象類,提供了一組通用的API

ContextIml.java類 路徑:/frameworks/base/core/java/android/app/ContextImpl.java

說明:Context的實作類,實作了Context的功能。注意:該類中大部分的功能都是直接調用其屬性mPackageInfo去完成的

ContextWrapper類 路徑 :/frameworks/base/core/java/android/content/ContextWrapper.java

說明:正如其名稱一樣,該類隻是對Context類的一種包裝,該類的構造函數包含了一個真正的Context引用,即ContextIml對象。

ContextThemeWrapper類 路徑:/frameworks/base/core/java/android/view/ContextThemeWrapper.java

說明:該類内部包含了主題(Theme)相關的接口,即android:theme屬性指定的。隻有Activity需要主題,Service不需要主題,是以Service直接繼承于ContextWrapper類。

Activity類 、Service類 、Application類本質上都是Context子類, 更多資訊大家可以自行參考源代碼進行了解。

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

下面介紹Context的一些get方法,通過這些get方法可以擷取應用環境全局資訊:

1.public abstract Context getApplicationContext ()

Return the context of the single, global Application object of the current process.

2.public abstract ApplicationInfo getApplicationInfo ()

Return the full application info for this context's package.

3.public abstract ContentResolver getContentResolver ()

Return a ContentResolver instance for your application's package.

4.public abstract PackageManager getPackageManager ()

Return PackageManager instance to find global package information.

5.public abstract String getPackageName ()

Return the name of this application's package.

6.public abstract Resources getResources ()

Return a Resources instance for your application's package.

7.public abstract SharedPreferences getSharedPreferences (String name, int mode)

Retrieve and hold the contents of the preferences file 'name', returning a SharedPreferences through which you can retrieve and modify its values. Only one instance of the SharedPreferences object is returned to any callers for the same name, meaning they will see each other's edits as soon as they are made.

8.public final String getString (int resId)

Return a localized string from the application's package's default string table.

9.public abstract Object getSystemService (String name)

Return the handle to a system-level service by name. The class of the returned object varies by the requested name. Currently available names are:

繼續閱讀