国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

Android 中Context的使用方法詳解

2019-10-22 18:27:13
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

Android 中Context的使用方法詳解

概要:

Context字面意思是上下文,位于framework package的android.content.Context中,其實(shí)該類為L(zhǎng)ONG型,類似Win32中的Handle句柄。很多方法需要通過(guò) Context才能識(shí)別調(diào)用者的實(shí)例:比如說(shuō)Toast的第一個(gè)參數(shù)就是Context,一般在Activity中我們直接用this代替,代表調(diào)用者的實(shí)例為Activity,而到了一個(gè)button的onClick(View view)等方法時(shí),我們用this時(shí)就會(huì)報(bào)錯(cuò),所以我們可能使用ActivityName.this來(lái)解決,主要原因是因?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)用級(jí)的操作,如啟動(dòng)Activity,broadcasting和接收intents。

下面介紹Context的一些get方法,通過(guò)這些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:

 還有很多有用的方法,具體不一一列舉。詳情請(qǐng)參考文檔,反正Context很有用。

如有疑問(wèn)請(qǐng)留言或到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到Android開(kāi)發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 内江市| 禹城市| 玉龙| 石泉县| 新竹县| 苍梧县| 南川市| 白河县| 司法| 云霄县| 连江县| 吐鲁番市| 教育| 安图县| 宣汉县| 岳池县| 贡山| 安宁市| 宜兴市| 隆林| 兴城市| 商都县| 黄梅县| 彝良县| 霸州市| 黔西县| 黄浦区| 六盘水市| 遂宁市| 榆林市| 陇南市| 紫阳县| 广昌县| 文山县| 邻水| 绵竹市| 海南省| 壶关县| 桂东县| 南丹县| 哈密市|