哪里是code这就是所谓的,当你调用getApplicationContext()? [英] Where is the code which is called when you call getApplicationContext()?
问题描述
我已经研究过的Android 来源的只是出于兴趣。我发现的是,上下文
是一个抽象类,抽象方法:
I have looked into Android sources just out of interest. What I found is that Context
is an abstract class with abstract method:
public abstract Context getApplicationContext();
在 ContextWrapper.java
扩展 Context.java
这导致执行 getApplicationContext(中)
方法:
The ContextWrapper.java
extends Context.java
which led to implementation of getApplicationContext()
method:
@Override
public Context getApplicationContext() {
return mBase.getApplicationContext();
}
但 MBASE
是引用类型的对象上下文
这是在初始化 ContextWrapper
的构造函数:
But mBase
is reference to an object of type Context
which is initialized in ContextWrapper
's constructor:
public ContextWrapper(Context base) {
mBase = base;
}
所以这个 MBASE
引用引用摘要
类?
好吧,我只是不明白的地方是,当你调用其上执行code getApplicationContext()
从活动
。
So this mBase
reference refers to abstract
class?
Well, I just don't understand where is the code which is executed when you call getApplicationContext()
from your Activity
.
推荐答案
这是多态的一个有趣的例子。
This is an interesting example of polymorphism.
只要你的基础
扩展上下文
,它必须提供的实施getApplicationContext()
,这在 ContextWrapper
的情况是,你在这里所提供的code。有本实施方式中,和<一href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.1.1_r1/android/app/ContextImpl.java#ContextImpl">the之一 ContextImpl
。
As long as your base
extends Context
, it has to provide an implementation of getApplicationContext()
, which in the case of ContextWrapper
is the code you provided here. There's this implementation, and the one in ContextImpl
.
重要的是要注意的一些事情读code时,你所提供的重要: ContextWrapper
本身继承上下文
,但它也需要一个上下文
作为输入(可以是一个 ContextWrapper
或服务
或应用程序
或活动
)。 ContextWrapper
不关心是哪一种;它只是知道他们有一个方法 getApplicationContext
,并希望当被问及调用该方法。 (例如,它可以通过另一个 ContextWrapper
,而是因为说 ContextWrapper
也需要一个上下文
在它的构造,这将只需添加嵌套的另一个层次。)
It's important to note a couple of things when reading the code you provided: ContextWrapper
itself extends Context
, but it also takes a Context
as an input (which could be a ContextWrapper
, or a Service
, or an Application
, or an Activity
). ContextWrapper
doesn't care which kind it is; it just knows that they have a method getApplicationContext
and it wants to call that method when asked. (For example, it could be passed another ContextWrapper
, but because said ContextWrapper
would also require a Context
in its constructor, that would just add another level of nesting.)
在应用程序扩展ContextWrapper
类调用超(空)
,这将意味着 getApplicationContext()
将抛出一个 NullPointerException异常
,如果它被留下这样的 - 但是,在 ContextWrapper
它也可设置为 attachBaseContext(上下文)
,这是它得到有趣。
The Application extends ContextWrapper
class calls super(null)
, which would mean that getApplicationContext()
would throw a NullPointerException
if it were left that way--however, in ContextWrapper
it's also settable by attachBaseContext(Context)
, and this is where it gets interesting.
两个活动
和应用程序
有方法附加(上下文[...其他的东西])
。他们每个人都要求 attachBaseContext()
与传入的上下文
。
Both Activity
and Application
have methods attach(Context [...other stuff])
. Each of them calls attachBaseContext()
with the passed-in Context
.
- 在
仪表
类,你会发现<一href="http://grep$c$c.com/search/usages?type=method&id=repository.grep$c$c.com%24java%24ext@com.google.android%24android@4.1.1_r1@android%24app@Instrumentation@newApplication%28java.lang.ClassLoader,java.lang.String,android.content.Context%29&k=u"><$c$c>android.app.Instrumentation.newApplication()$c$c>,其中ContextImpl
创建,并传递到应用程序
。 - 在
ActivityThread
类,你会发现<一href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.1.1_r1/android/app/ActivityThread.java#2008"><$c$c>handleBindApplication$c$c>它创建一个ContextImpl
获取传递给活动
作为其根上下文
。 - 在
LoadedApk
类,你会发现makeApplication
这将创建一个ContextImpl
获取传递给应用程序
。 <一href="http://grep$c$c.com/search/usages?type=method&id=repository.grep$c$c.com%24java%24ext@com.google.android%24android@4.1.1_r1@android%24app@LoadedApk@makeApplication%28boolean,android.app.Instrumentation%29&k=u">Here是它被称为地方。
- In the
Instrumentation
class, you'll findandroid.app.Instrumentation.newApplication()
, where aContextImpl
is created, and passed into anApplication
. - In the
ActivityThread
class, you'll findhandleBindApplication
which creates aContextImpl
that gets passed to theActivity
as its rootContext
. - In the
LoadedApk
class, you'll findmakeApplication
which creates aContextImpl
that gets passed to anApplication
. Here are the places it's called.
于是,在一天结束的时候, MBASE
通常最终成为一个 ContextImpl
。
So, at the end of the day, mBase
typically ends up as a ContextImpl
.
潜在有用的链接我看了一下,同时寻找这一切的:
Potentially useful links I looked at while finding all of this out:
- <一个href="http://grep$c$c.com/search/usages?type=method&id=repository.grep$c$c.com%24java%24ext@com.google.android%24android@4.1.1_r1@android%24app@Activity@attach%28android.content.Context,android.app.ActivityThread,android.app.Instrumentation,android.os.IBinder,int,android.app.Application,android.content.Intent,android.content.pm.ActivityInfo,java.lang.CharSequence,android.app.Activity,java.lang.String,android.app.Activity.NonConfigurationInstances,android.content.res.Configuration%29&k=u">Usages对
连接
- <一个href="http://grep$c$c.com/search/usages?type=method&id=repository.grep$c$c.com%24java%24ext@com.google.android%24android@4.1.1_r1@android%24content@ContextWrapper@attachBaseContext%28android.content.Context%29&k=u">Usages对
attachBaseContext
这篇关于哪里是code这就是所谓的,当你调用getApplicationContext()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!