Android 中Context的使用方法詳解
Android 中Context的使用方法詳解
概要:
Context字面意思是上下文,位于framework package的android.content.Context中,其實(shí)該類為LONG型,類似Win32中的Handle句柄。很多方法需要通過 Context才能識別調(diào)用者的實(shí)例:比如說Toast的第一個(gè)參數(shù)就是Context,一般在Activity中我們直接用this代替,代表調(diào)用者的實(shí)例為Activity,而到了一個(gè)button的onClick(View view)等方法時(shí),我們用this時(shí)就會報(bào)錯(cuò),所以我們可能使用ActivityName.this來解決,主要原因是因?yàn)閷?shí)現(xiàn)Context的類主要有Android特有的幾個(gè)模型,Activity以及Service。
Context提供了關(guān)于應(yīng)用環(huán)境全局信息的接口。它是一個(gè)抽象類,它的執(zhí)行被Android系統(tǒng)所提供。它允許獲取以應(yīng)用為特征的資源和類型。同時(shí)啟動(dòng)應(yīng)用級的操作,如啟動(dòng)Activity,broadcasting和接收intents。
下面介紹Context的一些get方法,通過這些get方法可以獲取應(yīng)用環(huán)境全局信息:
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:
還有很多有用的方法,具體不一一列舉。詳情請參考文檔,反正Context很有用。
如有疑問請留言或到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
- Android編程實(shí)現(xiàn)全局獲取Context及使用Intent傳遞對象的方法詳解
- Android全局獲取Context實(shí)例詳解
- Android編程實(shí)現(xiàn)為ListView創(chuàng)建上下文菜單(ContextMenu)的方法
- Android context源碼詳解及深入分析
- Android面試筆記之常問的Context
- 談?wù)凙ndroid里的Context的使用實(shí)例
- 避免 Android中Context引起的內(nèi)存泄露
- 安卓Android Context類實(shí)例詳解
- 詳解Android中的Context抽象類
- 深入解析Android App開發(fā)中Context的用法
- Android編程獲取全局Context的方法
- Android編程中context及全局變量實(shí)例詳解
- Android中ContextMenu用法實(shí)例
- android基礎(chǔ)教程之context使用詳解
- Android獲取其他包的Context實(shí)例代碼
- android中Context深入詳解
相關(guān)文章
[Android] 通過GridView仿微信動(dòng)態(tài)添加本地圖片示例代碼
本篇文章主要介紹了[Android] 通過GridView仿微信動(dòng)態(tài)添加本地圖片示例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-01-01
Android實(shí)現(xiàn)Banner界面廣告圖片循環(huán)輪播(包括實(shí)現(xiàn)手動(dòng)滑動(dòng)循環(huán))
這篇文章主要介紹了Android實(shí)現(xiàn)Banner界面廣告圖片循環(huán)輪播(包括實(shí)現(xiàn)手動(dòng)滑動(dòng)循環(huán))的相關(guān)資料,需要的朋友可以參考下2016-02-02
Android user版通過adb_enable開啟adb 調(diào)試 不提示對話框的流程分析
這篇文章主要介紹了Android user版通過adb_enable開啟adb 調(diào)試 不提示對話框的流程分析,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
android編程判斷應(yīng)用是否具有某個(gè)權(quán)限的方法
這篇文章主要介紹了android編程判斷應(yīng)用是否具有某個(gè)權(quán)限的方法,涉及Android進(jìn)程操作及權(quán)限控制的相關(guān)使用技巧,需要的朋友可以參考下2015-10-10
Android 中動(dòng)態(tài)加載.jar的實(shí)現(xiàn)步驟
本文介紹動(dòng)態(tài)加載 .jar的實(shí)現(xiàn)步驟,這將對你的android開發(fā)很有幫助,剛興趣的朋友可以了解下哦2013-01-01
Android5.0中Material Design的新特性
這篇文章主要介紹了Android5.0中Material Design的新特性的相關(guān)資料,需要的朋友可以參考下2016-08-08
Android實(shí)現(xiàn)頁面滑動(dòng)切換動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)頁面滑動(dòng)切換動(dòng)畫,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12

