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

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

问题描述

我无法找到一个满意的答案,所以在这里我们去:这是怎么回事与活动/ 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 仍然会返回一个不同的上下文实例(一个由机器人注入)。那是一个错误?是不是故意的?

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 活动服务定义,但不能在上下文?不应该有总是可以从一个有效的应用程序实例的的任何地方的?

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.

虽然在目前的Andr​​oid活动和服务实现, 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).

所以,如果你想你的清单中注册的应用程序类,则应永远呼叫 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()仅在活动类和服务类,而 getApplicationContext()是在Context类中声明。

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

这实际上意味着一件事:写在广播接收器,这是不是一个上下文,但鉴于其的onReceive方法上下文code的时候,你只能叫 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 code,可以看到连接的时候,一个活动都有基本内容和应用程序,而这些都是不同的参数。 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:

通常没有必要继承应用程序。在大多数情况下,   静态单身可以提供相同的功能在一个更模块化   方法。如果你单身需要一个全球范围内(例如注册   广播接收器),所述函数来检索它可以给出一个   上下文在内部使用了 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.

我知道这是不是一个确切的和precise的答案,不过,这是否回答你的问题?

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

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

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