在安卓开发的过程中,我们一开始就会接触到一个叫上下文的东西,也即Context,有时用this,有时用getApplication,还有的时候会有getContext代替,那么上下文究竟是什么的,上下文在Android开发中又起着什么样的作用呢,这里就一起探讨一下。
Context,中文直译为“上下文”,SDK中对其说明如下:Interface to global information about an application environment. This is an abstract class whose implementationis 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
从上可知以下三点,即:
1、它描述的是一个应用程序环境的信息,即上下文。
2、该类是一个抽象(abstract class)类,Android提供了该抽象类的具体实现类(后面我们会讲到是ContextIml类)。
3、通过它我们可以获取应用程序的资源和类,也包括一些应用级别操作
Android中的Context可以理解成场景,场景是什么什么意思呢?如果把Android程序的整个运行期间看成一部电影的话,那么一个Activity或者一个Service就可以称之为一个场景,一个电影可以包括多个场景。
咱们先看下Context相关的继承结构:
熟悉了Context的继承关系后,我们接下来分析应用程序在什么情况需要创建Context对象的?应用程序创建Context实例的情况有如下几种情况:
1、创建Application 对象时,而且整个App共一个Application对象
2、创建Service对象时
3、创建Activity对象时
因此应用程序App共有的Context数目公式为:
总Context实例个数 = Service个数 +Activity个数 + 1(Application对应的Context实例)
相关类介绍:
Context类 路径: /frameworks/base/core/java/android/content/Context.java
说明:抽象类,提供了一组通用的API
ContextIml.java类 路径:/frameworks/base/core/java/android/app/ContextImpl.java
说明:Context的实现类,实现了Context的功能。注意:该类中大部分的功能都是直接调用其属性mPackageInfo去完成的
ContextWrapper类 路径 :/frameworks/base/core/java/android/content/ContextWrapper.java
说明:正如其名称一样,该类只是对Context类的一种包装,该类的构造函数包含了一个真正的Context引用,即ContextIml对象。
ContextThemeWrapper类 路径:/frameworks/base/core/java/android/view/ContextThemeWrapper.java
说明:该类内部包含了主题(Theme)相关的接口,即android:theme属性指定的。只有Activity需要主题,Service不需要主题,所以Service直接继承于ContextWrapper类。
Activity类 、Service类 、Application类本质上都是Context子类, 更多信息大家可以自行参考源代码进行理解。
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
下面介绍Context的一些get方法,通过这些get方法可以获取应用环境全局信息:
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: