在android中, Context表示运行时上下文,其类图为:
由图中可以看到Activity、Service、Application都是继承ContextWrapper,而ContextWrapper类实现了Context接口。其中ContextWrapper是Context的包装类,真正的实现类为ContextIml,ContextWrapper类有个成员变量:
public class ContextWrapper extends Context {
Context mBase;
public ContextWrapper(Context base) {
mBase = base;
}
/**
* Set the base context for this ContextWrapper. All calls will then be
* delegated to the base context. Throws
* IllegalStateException if a base context has already been set.
*
* @param base The new base context for this wrapper.
*/
protected void attachBaseContext(Context base) {
if (mBase != null) {
throw new IllegalStateException("Base context already set");
}
mBase = base;
}
其中mBase为ContextIml类的实例,ContextImpl类有内部类ServiceFetcher和StaticServiceFetcher类,
/*package*/ sta