SDK Doc中对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
看代码,可以知道Context是一个抽象类,Android系统提供的实现是ContextImpl,它提供了对应用所执行的环境操作的功能,Context提供的功能包括:
- Start/Stop Service
- bind/unbind Service
- register/unregister Receiver
- start intentsender
- start instrumentation
- start Activity
- Open/Create database
- open file
- . . . . .
从这个类图关系,我们可以看出,没创建一个Activity、Service或者Application都会创建一个ContextImpl对象,但是多个ContextImpl中大部分操作都是由一个PackageInfo对象完成,也就是说不论创建多少Context,最后执行操作的都是一个PackageInfo对象。