? 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."
“是一個用于實現(xiàn)關(guān)于應(yīng)用環(huán)境的整體信息的一個接口。這是一個由安卓系統(tǒng)提供的抽象類并且系統(tǒng)有對他進行實現(xiàn)。它允許訪問到應(yīng)用特殊的資源和類,同時它也可以實現(xiàn)到應(yīng)用級別的操作,例如:Activity的啟動,廣播的實現(xiàn)和接受intent等?!?/i>
一、Context的主要實現(xiàn)和繼續(xù)理解
? 知道了context的大概描述,我們可以再繼續(xù)理解Context這個神秘的對象了,首先,作為基類,肯定有其它類去實現(xiàn)它,主要實現(xiàn)了context類的類是Activity,Service,Application。他們?nèi)齻€類雖然都是Context的子類,但是具體的繼承關(guān)系卻有些不大一樣:
Activity的繼承關(guān)系:
Service和Application的繼承關(guān)系:
? 可以看出我們的Context其實就是我們熟知的Activity,Service,Application。
? 在這3個類中,Activity的context對象和Application的context對象最容易弄混淆。
二、Context中的主要方法
? 知道了Context的大概描述和他的一些繼承關(guān)系,我們對Context這個類有了一個大致的了解?,F(xiàn)在可以看看在context中的一些方法,來加深對context的一個理解,有很多我們使用過的方法其實都是從Context這個類中實現(xiàn)而來。
? 我們從Android api中查看Context類,這里出現(xiàn)了一個非常熟悉的方法:startActivity,可以看到其實Activity中的StartActivity方法是重寫了Context中的方法。
abstract voidstartActivity(Intentintent)
Same asstartActivity(Intent, Bundle)with no options specified.
abstract voidstartActivity(Intentintent,Bundleoptions)
Launch a new activity.
同時context還可以訪問到資源文件,獲得資源文件中的信息。
abstractResourcesgetResources()
Return a Resources instance for your application's package.
abstractSharedPreferencesgetSharedPreferences(Stringname, int mode)
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,同時還可以開啟或者關(guān)閉一個Service。
abstractComponentNamestartService(Intentservice)
Request that a given application service be started.
abstract booleanstopService(Intentservice)
Request that a given application service be stopped.
? 訪問Android Api 或者查看源碼可以看到,Context中還有很多訪問資源文件和程序之間互相通信的方法。
? 可以看出context其實就是一個應(yīng)用之中的手腳,可以通過他來拿取資源文件中的資源,還可以通過他來處理Activity和Service中的一些操作,這個類就是整個程序的樞紐,負責(zé)管理整個程序的通暢運行。
? 我們可以通過分析一個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去調(diào)用系統(tǒng)寫好的資源文件來進行實現(xiàn)的。
三、Activity context和Application context的區(qū)別
? Activity的context和Application的context的區(qū)別在于生命周期的區(qū)別,Activity的context是依附在著Activity的生命周期的,而Application的Context的生命周期是依附在整個應(yīng)用之上的。