Framework基础:Context是如何跟ContextImpl关联的

Framework基础:Context是如何跟ContextImpl关联的_第1张图片
蒲公英的约定.png

Context是场景的意思,android有什么场景呢?最常用的场景就是Activity了。Context让对象要一个共同的抓手。例如一个Activity叫中国,那么里面的对象中国人就都要一个共同抓手国家。

下面是系统Context的类图

Framework基础:Context是如何跟ContextImpl关联的_第2张图片
Context类图.png

从上面的图可以看到,Activity和ContextImpl并没有直接的继承关系,而我们知道Activity的主要处理逻辑都在ContextImpl里面。下面我们看看是怎么一步步引用到ContextImpl的。

以Activity获取系统服务PowerManager为例简单说一下
源码路径/frameworks/base/core/java/android/app/Activity.java
在Activity的oncreate中

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
      PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
}

进入Activity的getSystemService

    @Override
    public Object getSystemService(@ServiceName @NonNull String name) {
        if (getBaseContext() == null) {
            throw new IllegalStateException(
                    "System services not available to Activities before onCreate()");
        }

        if (WINDOW_SERVICE.equals(name)) {
            return mWindowManager;
        } else if (SEARCH_SERVICE.equals(name)) {
            ensureSearchManager();
            return mSearchManager;
        }
        return super.getSystemService(name);
    }

super.getSystemService(name)会进入父类的getSystemService方法,父类由上面的类图可以知道是ContextThemeWrapper
源码路径:/frameworks/base/core/java/android/view/ContextThemeWrapper.java

    @Override public Object getSystemService(String name) {
        if (LAYOUT_INFLATER_SERVICE.equals(name)) {
            if (mInflater == null) {
                mInflater = LayoutInflater.from(getBaseContext()).cloneInContext(this);
            }
            return mInflater;
        }
        return getBaseContext().getSystemService(name);
    }

如果不是LAYOUT_INFLATER_SERVICE的话,会进入getBaseContext().getSystemService(name);
查看getBaseContext()方法,该方法在ContextThemeWrapper的父类ContextWrapper中定义

  Context mBase;
    public Context getBaseContext() {
        return mBase;
    }

    protected void attachBaseContext(Context base) {
        if (mBase != null) {
            throw new IllegalStateException("Base context already set");
        }
        mBase = base;
    }

可以看到返回mBase变量,进而调用mBase的getSystemService方法获取到系统,所以现在的关键是查看这个mBase是何方神圣。
这个变量是通过函数attachBaseContext赋值,这个函数在Activity类被attach方法调用

    final void attach(Context context, ActivityThread aThread,
            Instrumentation instr, IBinder token, int ident,
            Application application, Intent intent, ActivityInfo info,
            CharSequence title, Activity parent, String id,
            NonConfigurationInstances lastNonConfigurationInstances,
            Configuration config, String referrer, IVoiceInteractor voiceInteractor) {
        .......
        attachBaseContext(context);
        ......
    }

这个方法在ActivityThread类中的performLaunchActivity方法调用,这个方法用于启动Activity,所以mBase对象是在应用启动的时候初始化的。
源码路径:/frameworks/base/core/java/android/app/ActivityThread.java

    private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
                ......
                Context appContext = createBaseContextForActivity(r, activity);
                activity.attach(appContext, this, getInstrumentation(), r.token,
                        r.ident, app, r.intent, r.activityInfo, title, r.parent,
                        r.embeddedID, r.lastNonConfigurationInstances, config,
                        r.referrer, r.voiceInteractor);
                ........

    }

马上就能知道这个mBase对象是什么了,看看createBaseContextForActivity函数

    private Context createBaseContextForActivity(ActivityClientRecord r, final Activity activity) {
        ..........
        ContextImpl appContext = ContextImpl.createActivityContext(
                this, r.packageInfo, displayId, r.overrideConfig);
        Context baseContext = appContext;
        ..............
        return baseContext;
    }

看到了吧,createBaseContextForActivity返回的是ContextImpl类型的对象。所以mBase对象类型就是ContextImpl 。Activity成功与ContextImpl 联系起来了!!

上面的说明有点逆向,用时间顺序来说的话,就是应用启动过程在performLaunchActivity中创建了ContextImpl类型的mBase变量,然后将mBase attach到Activity中,然后应用调用getSystemService的时候,会逐步调用到mBase的getSystemService方法,即ContextImpl类的getSystemService方法。

Framework基础:Context是如何跟ContextImpl关联的_第3张图片
getSystemService调用图.png

你可能感兴趣的:(Framework基础:Context是如何跟ContextImpl关联的)