
上QQ阅读APP看本书,新人免费读10天
设备和账号都新为新人
5.3 Application Context的获取过程
当我们熟知了Application Context的创建过程之后,那么它的获取过程会非常好理解。我们通过调用getApplicationContext方法来获得Application Context,getApplicationContext方法在ContextWrapper中实现,如下所示:

mBase指的是ContextImpl,我们来查看ContextImpl的getApplicationContext方法:

如果LoadedApk类型的mPackageInfo不为null,则调用LoadedApk的getApplication方法,否则调用AvtivityThread的getApplication方法。由于应用程序这时已经启动,因此LoadedApk不会为null,则会调用LoadedApk的getApplication方法,如下所示:

这里的mApplication我们应该很熟悉,它在上文LoadedApk的makeApplication方法的注释5处被赋值。这样我们通过getApplicationContext方法就获取到了Application Context。