




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、 Context類,我們時刻的在與它打交道,例如:Service、BroadcastReceiver、Activity等都會利用到Context的相關(guān)方法。但是不懂Context的原理、類結(jié)構(gòu)關(guān)系。Contenxt到底是什么,一個應(yīng)用程序App中存在多少個Context實例對象呢?在這里我們就來了解一下Context。在API中Context的解釋是:Interface to global information about an application environment. This is an abstract class whose implementation is provide
2、d 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、它描述的是一個應(yīng)用程序環(huán)境的信息,即上下文。2、該類是一個抽象(abstract class)類,Android提供了該抽象類的具體實現(xiàn)類3、通過它我
3、們可以獲取應(yīng)用程序的資源和類,也包括一些應(yīng)用級別操作,例如:啟動一個Activity,發(fā)送廣播,接受Intent信息 等。從以上分析可以看出,Context就是一個對APK包無所不知的大管家,大家需要什么,直接問它就可以了。一、Context類的相關(guān)類的繼承關(guān)系可以看到Activity、Service、Application都是Context的子類;也就是說,Android系統(tǒng)的角度來理解:Context是一個場景,代表與操作系統(tǒng)的交互的一種過程。從程序的角度上來理解:Context是個抽象類,而Activity、Service、Application等都是該類的一個實現(xiàn)。在仔細看一下上圖:A
4、ctivity、Service、Application都是繼承自ContextWrapper,而ContextWrapper內(nèi)部會包含一個base context,由這個base context去實現(xiàn)了絕大多數(shù)的方法。另外,View與Context(或Activity)的關(guān)系類似于明星與經(jīng)紀人的關(guān)系,所以創(chuàng)建View時,必須明確指定其Context(即經(jīng)紀人或大管家),否則View就成不了明星。2、 Context的關(guān)鍵函數(shù)以及各子類的特征1.Context是上下文的最基本的類;2.ContextWrapper是對Context類的一種封裝,它的構(gòu)造函數(shù)包含了一個真正的Context引用,即C
5、ontextImpl對象;3.ContextThemeWrapper類內(nèi)部包含了主題(Theme)相關(guān)的接口,即android:theme屬性指定的。只有Activity需要主題,Service不需要主題,所以Service直接繼承于ContextWrapper類以下是Context的常用方法介紹:public abstract class Context / 獲取應(yīng)用程序包的AssetManager實例public abstract AssetManager getAssets();/ 獲取應(yīng)用程序包的Resources實例public abstract Resources getResou
6、rces();/ 獲取PackageManager實例,以查看全局package信息public abstract PackageManager getPackageManager();/ 獲取應(yīng)用程序包的ContentResolver實例public abstract ContentResolver getContentResolver();/ 它返回當前進程的主線程的Looper,此線程分發(fā)調(diào)用給應(yīng)用組件(activities, services等)public abstract Looper getMainLooper();/ 返回當前進程的單實例全局Application對象的Cont
7、extpublic abstract Context getApplicationContext();/ 從string表中獲取本地化的、格式化的字符序列public final CharSequence getText(int resId) return getResources().getText(resId);/ 從string表中獲取本地化的字符串public final String getString(int resId) return getResources().getString(resId);public final String getString(int resId,
8、Object. formatArgs) return getResources().getString(resId, formatArgs);/ 返回一個可用于獲取包中類信息的class loaderpublic abstract ClassLoader getClassLoader();/ 返回應(yīng)用程序包名public abstract String getPackageName();/ 返回應(yīng)用程序信息public abstract ApplicationInfo getApplicationInfo();/ 根據(jù)文件名獲取SharedPreferencespublic abstract
9、SharedPreferences getSharedPreferences(String name,int mode);/ 其根目錄為: Environment.getExternalStorageDirectory()/* param type The type of files directory to return. May be null for* the root of the files directory or one of* the following Environment constants for a subdirectory:* link android.os.Env
10、ironment#DIRECTORY_MUSIC,* link android.os.Environment#DIRECTORY_PODCASTS,* link android.os.Environment#DIRECTORY_RINGTONES,* link android.os.Environment#DIRECTORY_ALARMS,* link android.os.Environment#DIRECTORY_NOTIFICATIONS,* link android.os.Environment#DIRECTORY_PICTURES, or* link android.os.Envir
11、onment#DIRECTORY_MOVIES.*/public abstract File getExternalFilesDir(String type);/ 返回應(yīng)用程序obb文件路徑public abstract File getObbDir();/ 啟動一個新的activitypublic abstract void startActivity(Intent intent);/ 啟動一個新的activitypublic void startActivityAsUser(Intent intent, UserHandle user) throw new RuntimeException
12、(Not implemented. Must override in a subclass.);/ 啟動一個新的activity/ intent: 將被啟動的activity的描述信息/ options: 描述activity將如何被啟動public abstract void startActivity(Intent intent, Bundle options);/ 啟動多個新的activitypublic abstract void startActivities(Intent intents);/ 啟動多個新的activitypublic abstract void startActi
13、vities(Intent intents, Bundle options);/ 廣播一個intent給所有感興趣的接收者,異步機制public abstract void sendBroadcast(Intent intent);/ 廣播一個intent給所有感興趣的接收者,異步機制public abstract void sendBroadcast(Intent intent,String receiverPermission);public abstract void sendOrderedBroadcast(Intent intent,String receiverPermission
14、);public abstract void sendOrderedBroadcast(Intent intent,String receiverPermission, BroadcastReceiver resultReceiver,Handler scheduler, int initialCode, String initialData,Bundle initialExtras);public abstract void sendBroadcastAsUser(Intent intent, UserHandle user);public abstract void sendBroadca
15、stAsUser(Intent intent, UserHandle user,String receiverPermission);/ 注冊一個BroadcastReceiver,且它將在主activity線程中運行public abstract Intent registerReceiver(BroadcastReceiver receiver,IntentFilter filter);public abstract Intent registerReceiver(BroadcastReceiver receiver,IntentFilter filter, String broadcas
16、tPermission, Handler scheduler);public abstract void unregisterReceiver(BroadcastReceiver receiver);/ 請求啟動一個application servicepublic abstract ComponentName startService(Intent service);/ 請求停止一個application servicepublic abstract boolean stopService(Intent service);/ 連接一個應(yīng)用服務(wù),它定義了application和service間
17、的依賴關(guān)系public abstract boolean bindService(Intent service, ServiceConnection conn,int flags);/ 斷開一個應(yīng)用服務(wù),當服務(wù)重新開始時,將不再接收到調(diào)用,/ 且服務(wù)允許隨時停止public abstract void unbindService(ServiceConnection conn);/ 返回系統(tǒng)級service句柄/* see #WINDOW_SERVICE* see android.view.WindowManager* see #LAYOUT_INFLATER_SERVICE* see andr
18、oid.view.LayoutInflater* see #ACTIVITY_SERVICE* see android.app.ActivityManager* see #POWER_SERVICE* see android.os.PowerManager* see #ALARM_SERVICE* see android.app.AlarmManager* see #NOTIFICATION_SERVICE* see android.app.NotificationManager* see #KEYGUARD_SERVICE* see android.app.KeyguardManager*
19、see #LOCATION_SERVICE* see android.location.LocationManager* see #SEARCH_SERVICE* see android.app.SearchManager* see #SENSOR_SERVICE* see android.hardware.SensorManager* see #STORAGE_SERVICE* see android.os.storage.StorageManager* see #VIBRATOR_SERVICE* see android.os.Vibrator* see #CONNECTIVITY_SER
20、VICE* see .ConnectivityManager* see #WIFI_SERVICE* see .wifi.WifiManager* see #AUDIO_SERVICE* see android.media.AudioManager* see #MEDIA_ROUTER_SERVICE* see android.media.MediaRouter* see #TELEPHONY_SERVICE* see android.telephony.TelephonyManager* see #INPUT_METHOD_SERVICE* see android.view.inputmet
21、hod.InputMethodManager* see #UI_MODE_SERVICE* see android.app.UiModeManager* see #DOWNLOAD_SERVICE* see android.app.DownloadManager*/public abstract Object getSystemService(String name);public abstract int checkPermission(String permission, int pid, int uid);/ 返回一個新的與application name對應(yīng)的Context對象publ
22、ic abstract Context createPackageContext(String packageName,int flags) throws PackageManager.NameNotFoundException;/ 返回基于當前Context對象的新對象,其資源與display相匹配public abstract Context createDisplayContext(Display display);三、什么時候會創(chuàng)建Context實例 熟悉了Context的繼承關(guān)系后,我們接下來分析應(yīng)用程序在什么情況需要創(chuàng)建Context對象的?應(yīng)用程序創(chuàng)建Context實例的情況有如
23、下幾種情況: 1、創(chuàng)建Application 對象時, 而且整個App共用一個Application對象,生命周期同整個應(yīng)用 2、創(chuàng)建Service對象時 3、創(chuàng)建Activity對象時,生命周期同activity 因此應(yīng)用程序App共有的Context數(shù)目公式為:總Context實例個數(shù) = Service個數(shù) + Activity個數(shù) + 1(Application對應(yīng)的Context實例)四、Context的應(yīng)用場景,下表說明的是各種不同的上下文對象能做的事情有哪些大家注意看到有一些NO上添加了一些數(shù)字,其實這些從能力上來說是YES,但是為什么說是NO呢?下面一個一個解釋:數(shù)字1:啟動Activity在這些類中是可以的,但是需要創(chuàng)建一個新的
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 建筑工程違約合同條款
- 合作協(xié)議要求交保證金
- 醫(yī)務(wù)勞動合同范本
- 企業(yè)預(yù)存消費合同范本
- 養(yǎng)羊協(xié)議合同范本
- 公司柴油采購合同范本
- 出書授權(quán)合同范本
- 口罩廠勞動合同范本
- 2025周黑鴨合作伙伴關(guān)系管理策劃書
- 醫(yī)用鋼釘采購合同范例
- 固定翼無人機飛行原理課件
- 《母嬰護理》課程標準
- 軟基處治面積四方聯(lián)測測量記錄表(清淤后)
- 考試通用答題卡word模板
- 尾礦庫安全檢查表
- 解除、終止勞動合同通知書范本
- 勞動定額定員標準化1(孫義敏)
- 深信服桌面云方案
- 2021年深圳實驗學(xué)校初中部七年級入學(xué)分班考試數(shù)學(xué)試卷及答案解析
- ??怂箍等鴺藴y量儀的使用課件
- 高血壓臨床路徑
評論
0/150
提交評論