getApplication() 与 getApplicationContext() [英] getApplication() vs. getApplicationContext()

查看:19
本文介绍了getApplication() 与 getApplicationContext()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到满意的答案,所以我们开始:Activity/Service.getApplication()Context.getApplicationContext() 有什么关系?

I couldn't find a satisfying answer to this, so here we go: what's the deal with Activity/Service.getApplication() and Context.getApplicationContext()?

在我们的应用程序中,两者都返回相同的对象.然而,在 ActivityTestCase 中,模拟应用程序将使 getApplication() 与模拟一起返回,但 getApplicationContext 仍将返回不同的上下文实例(一种由 Android 注入).这是一个错误吗?是故意的吗?

In our application, both return the same object. In an ActivityTestCase however, mocking the application will make getApplication() come back with the mock, but getApplicationContext will still return a different context instance (one injected by Android). Is that a bug? Is it on purpose?

一开始我什至不明白其中的区别.在测试套件之外是否存在两个调用可能返回不同对象的情况?什么时候以及为什么?此外,为什么 getApplication 定义在 ActivityService 上,而不定义在 Context 上?任何地方不应该总是有一个有效的应用程序实例吗?

I don't even understand the difference in the first place. Are there cases outside a test suite where both calls may come back with different objects? When and why? Moreover, why is getApplication defined on Activity and Service, but not on Context? Shouldn't there always be a valid application instance available from anywhere?

推荐答案

非常有趣的问题.我觉得主要是语义,也可能是历史原因.

Very interesting question. I think it's mainly a semantic meaning, and may also be due to historical reasons.

尽管在当前的 Android Activity 和 Service 实现中,getApplication()getApplicationContext() 返回相同的对象,但不能保证总是如此(例如,在特定的供应商实现中).

Although in current Android Activity and Service implementations, getApplication() and getApplicationContext() return the same object, there is no guarantee that this will always be the case (for example, in a specific vendor implementation).

因此,如果您想要在 Manifest 中注册的 Application 类,您应该永远调用 getApplicationContext() 并将其转换为您的应用程序,因为它可能不是应用程序实例(您显然在测试框架中经历过).

So if you want the Application class you registered in the Manifest, you should never call getApplicationContext() and cast it to your application, because it may not be the application instance (which you obviously experienced with the test framework).

为什么 getApplicationContext() 首先存在?

getApplication() 仅在 Activity 类和 Service 类中可用,而 getApplicationContext() 在 Context 类中声明.

getApplication() is only available in the Activity class and the Service class, whereas getApplicationContext() is declared in the Context class.

这实际上意味着一件事:在广播接收器中编写代码时,它不是上下文,而是在其 onReceive 方法中给出了上下文,您只能调用 getApplicationContext().这也意味着不能保证您可以在 BroadcastReceiver 中访问您的应用程序.

That actually means one thing : when writing code in a broadcast receiver, which is not a context but is given a context in its onReceive method, you can only call getApplicationContext(). Which also means that you are not guaranteed to have access to your application in a BroadcastReceiver.

查看 Android 代码时,您会看到附加的 Activity 会收到一个基本上下文和一个应用程序,它们是不同的参数.getApplicationContext() 将它的调用委托给 baseContext.getApplicationContext().

When looking at the Android code, you see that when attached, an activity receives a base context and an application, and those are different parameters. getApplicationContext() delegates it's call to baseContext.getApplicationContext().

还有一件事:文档说在大多数情况下,您不需要子类化应用程序:

One more thing : the documentation says that it most cases, you shouldn't need to subclass Application:

通常不需要子类Application.大多数情况下,静态单例可以以更模块化的方式提供相同的功能道路.如果您的单身人士需要全局上下文(例如注册广播接收器),可以给出检索它的函数Context 内部使用 Context.getApplicationContext() 时首先构造单例.

There is normally no need to subclass Application. In most situation, static singletons can provide the same functionality in a more modular way. If your singleton needs a global context (for example to register broadcast receivers), the function to retrieve it can be given a Context which internally uses Context.getApplicationContext() when first constructing the singleton.

我知道这不是一个准确而准确的答案,但仍然可以回答您的问题吗?

I know this is not an exact and precise answer, but still, does that answer your question?

这篇关于getApplication() 与 getApplicationContext()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆