Android中有個我們熟悉又陌生的對象Context(上下文),當我們啟動Activity的時候需要上下文,當我們使用dialog的時候我們需要上下文,但是上下文對象到底是個什麼東西呢?
在Android api當中是這樣描述context對象的。
"Interface to global information about an application environment. This is an abstract class whose implementation is 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."
“是一個用于實作關于應用環境的整體資訊的一個接口。這是一個由安卓系統提供的抽象類并且系統有對他進行實作。它允許通路到應用特殊的資源和類,同時它也可以實作到應用級别的操作,例如:Activity的啟動,廣播的實作和接受intent等。”
一、Context的主要實作和繼續了解
知道了context的大概描述,我們可以再繼續了解Context這個神秘的對象了,首先,作為基類,肯定有其它類去實作它,主要實作了context類的類是Activity,Service,Application。他們三個類雖然都是Context的子類,但是具體的繼承關系卻有些不大一樣:
Activity的繼承關系:
Service和Application的繼承關系:
可以看出我們的Context其實就是我們熟知的Activity,Service,Application。
在這3個類中,Activity的context對象和Application的context對象最容易弄混淆。
二、Context中的主要方法
知道了Context的大概描述和他的一些繼承關系,我們對Context這個類有了一個大緻的了解。現在可以看看在context中的一些方法,來加深對context的一個了解,有很多我們使用過的方法其實都是從Context這個類中實作而來。
我們從Android api中檢視Context類,這裡出現了一個非常熟悉的方法:startActivity,可以看到其實Activity中的StartActivity方法是重寫了Context中的方法。
abstract voidstartActivity(Intentintent)
Same asstartActivity(Intent, Bundle)with no options specified.
abstract voidstartActivity(Intentintent,Bundleoptions)
Launch a new activity.
同時context還可以通路到資源檔案,獲得資源檔案中的資訊。
Return a Resources instance for your application's package.
Retrieve and hold the contents of the preferences file 'name', returning a SharedPreferences through which you can retrieve and modify its values.
finalStringgetString(int resId)
Return a localized string from the application's package's default string table.
finalStringgetString(int resId,Object...formatArgs)
Return a localized formatted string from the application's package's default string table, substituting the format arguments as defined inFormatterandformat(String, Object...).
同時context不但可以開啟一個activity,同時還可以開啟或者關閉一個Service。
Request that a given application service be started.
abstract booleanstopService(Intentservice)
Request that a given application service be stopped.
通路Android Api 或者檢視源碼可以看到,Context中還有很多通路資源檔案和程式之間互相通信的方法。
可以看出context其實就是一個應用之中的手腳,可以通過他來拿取資源檔案中的資源,還可以通過他來處理Activity和Service中的一些操作,這個類就是整個程式的樞紐,負責管理整個程式的通暢運作。
我們可以通過分析一個Toast通知的源碼去分析context的去向和使用,來了解context到底做了些神馬操作:
public static Toast makeText(Context context, CharSequence text, int duration) {
Toast result = new Toast(context);
LayoutInflater inflate = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null);
TextView tv = (TextView)v.findViewById(com.android.internal.R.id.message);
tv.setText(text);
result.mNextView = v;
result.mDuration = duration;
return result;
}
可以看到makeText方法接受的context被用于
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null);
這是用于擷取XML中定義的View的方法,可以看到通過外部傳入的Context,在這裡獲得了一個View布局用于顯示Toast。
public Toast(Context context) {
mContext = context;
mTN = new TN();
mTN.mY = context.getResources().getDimensionPixelSize(
com.android.internal.R.dimen.toast_y_offset);
mTN.mGravity = context.getResources().getInteger(
com.android.internal.R.integer.config_toastDefaultGravity);
}
這一行中可以看出在context又被用來擷取資源檔案,可以看出Toast的顯示和布局都是通過context去調用系統寫好的資源檔案來進行實作的。
三、Activity context和Application context的差別
Activity的context和Application的context的差別在于生命周期的差別,Activity的context是依附在着Activity的生命周期的,而Application的Context的生命周期是依附在整個應用之上的。