AndroidContext简介

Context字面意思是上下文,位于framework packageandroid.content.Context中,其实该类为LONG型,类似Win32中的Handle句柄。很多方法需要通过 Context才能识别调用者的实例:比如说Toast的第一个参数就是Context,一般在Activity中我们直接用this代替,代表调用者的实例为Activity,而到了一个buttononClick(View view)等方法时,我们用this时就会报错,所以我们可能使用ActivityName.this来解决,主要原因是因为实现Context的类主要有Android特有的几个模型,Activity以及Service

Context提供了关于应用环境全局信息的接口。它是一个抽象类,它的执行被Android系统所提供。它允许获取以应用为特征的资源和类型。同时启动应用级的操作,如启动Activitybroadcasting和接收intents

下面介绍Context的一些get方法,通过这些get方法可以获取应用环境全局信息:

1.public abstract ContextgetApplicationContext()

Return the context of the single, global Application object of the current process.

2.public abstract ApplicationInfogetApplicationInfo()

Return the full application info for this context's package.

3.public abstract ContentResolvergetContentResolver()

Return a ContentResolver instance for your application's package.

4.public abstract PackageManagergetPackageManager()

Return PackageManager instance to find global package information.

5.public abstract StringgetPackageName()

Return the name of this application's package.

6.public abstract ResourcesgetResources()

Return a Resources instance for your application's package.

7.public abstract SharedPreferencesgetSharedPreferences(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 StringgetString(int resId)

Return a localized string from the application's package's default string table.

9.public abstract ObjectgetSystemService(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很有用。

更多相关文章

  1. android 混淆配置 实例实战项目讲解
  2. Android禁止横屏竖屏切换的有效方法
  3. Android 只开启一个Activity实例
  4. 知识梳理:Android子线程中更新UI的3种方法
  5. OpenCV android sdk配置OpenCV android NDK开发实例
  6. android中判断当前运行activity名的方法
  7. 葵花宝典之普及篇 —— HD2 android 各种直刷方法
  8. android中实现view的更新UI有两组方法的问题
  9. AIDL实例分析和讲解

随机推荐

  1. 一个支持ListView的底部弹出控件 PopupWi
  2. android 之 View
  3. Android的生命周期中的方法的执行场景
  4. Android(安卓)Volley运用
  5. Android菜鸡提升之路---android你应该知
  6. Android(安卓)源码下利用jni编译自己的项
  7. android 客制化系统签名文件
  8. android 反编译(apk 源代码)
  9. Android代码优化----PullToRefresh+unive
  10. android 动态设置控件的高度,使用对应布局