从程序的角度上来理解:Context是个抽象类,而Activity、Service、Application等都是该类的一个实现。
查看类的继承关系:ctrl + H (Windows系统)
应用在三种情况下会创建Context对象(即通常说的context):
1> 创建Application 对象时,即第一次启动app时。 整个App共一个Application对象,所以也只有一个Application 的Context,Application销毁,它也销毁;
2> 创建Activity对象时。Activity销毁,它也销毁;
3> 创建Service对象时。Service销毁,它也销毁。
由此可以得到应用程序App可以创建的Context(Activity和Service没启动就不会创建)个数公式一般为:
总Context实例个数 = Service个数 + Activity个数 + 1(Application对应的Context对象)
================================================================================
/**
-
Interface to global information about an application environment. This is
-
an abstract class whose implementation is 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.
*/
public abstract class Context {
/**
-
File creation mode: the default mode, where the created file can only
-
be accessed by the calling application (or all applications sharing the
-
same user ID).
-
@see #MODE_WORLD_READABLE
-
@see #MODE_WORLD_WRITEABLE
*/
public static final int MODE_PRIVATE = 0x0000;
public static final int MODE_WORLD_WRITEABLE = 0x0002;
public static final int MODE_APPEND = 0x8000;
public static final int MODE_MULTI_PROCESS = 0x0004;
.
.
.
}
源码中的注释是这么来解释Context的:Context提供了关于应用环境全局信息的接口。它是一个抽象类,它的执行被Android系统所提供。它允许获取以应用为特征的资源和类型,是一个统领一些资源(应用程序环境变量等)的上下文。就是说,它描述一个应用程序环境的信息(即上下文);是一个抽象类,Android