获取应用程序上下文 [英] Getting the Application Context
问题描述
这可能是一个简单的问题,但我只是想确认我是对的。
This might be a simple question but I just wanted to make sure I am right.
在我的Android应用程序,我有一个使用一个构造器:
In my android application I have a constructor that uses:
activity.getApplicationContext()
的活性被传递到构造为一个参数。
The activity is passed into the constructor as a parameter.
现在的问题是,我调用这个类的服务。如果我让其中接受服务作为参数,并使用 service.getApplicationContext
第二构造?我会得到同样的应用程序上下文?
The problem is that I am calling this class from a Service. If I make a second constructor which accepts the Service as a parameter and uses service.getApplicationContext
? Will I get the same application context?
推荐答案
我会得到同样的应用程序上下文?的
是的。如果u可以检查机器人的文件,他们给了
Yes. if u can check the document of android they have given
getApplicationContext()
返回当前进程的单一的全球性应用对象的上下文。
所以它不应该被整个申请过程中改变。
so it should not be changed for the whole application process.
最重要的是你应该阅读
getApplicationContext()
一般只应使用,如果你需要一个上下文的生命周期独立于当前的背景下,是联系在一起的一生的过程,而不是目前的组件。的
getApplicationContext()
generally should only be used if you need a Context whose lifecycle is separate from the current context, that is tied to the lifetime of the process rather than the current component.
纠正我,如果我错了。
感谢
这篇关于获取应用程序上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!