获取应用程序上下文
这可能是一个简单的问题,但我只是想确保我是对的。
在我的 android 应用程序中,我有一个构造函数,它使用:
activity.getApplicationContext()
活动作为参数传递到构造函数中。
问题是我从服务中调用此类。如果我创建第二个构造函数,它接受 Service 作为参数并使用 service.getApplicationContext
?我会得到相同的应用程序上下文吗?
This might be a simple question but I just wanted to make sure I am right.
In my android application I have a constructor that uses:
activity.getApplicationContext()
The activity is passed into the constructor as a parameter.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
获取应用程序上下文的最简单方法是:
创建一个扩展
android.app.Application
的类App
修改标记具有属性
AndroidManifest.xml
的 < code>android:name="your.package.name.App"
。任何时候您需要应用程序上下文,只需从
App.context
获取即可。无论您的进程是否运行,无论它是活动、服务还是其他内容,
Application
始终首先初始化。您将始终可以访问应用程序上下文。The easiest way to get the application context is:
Create a class
App
that extendsandroid.app.Application
Modify your
AndroidManifest.xml
's<application>
tag to have the attributeandroid:name="your.package.name.App"
.Any time you need the application context, just get it from
App.context
.Application
is always initialized first whether your process runs, whether it's an activity, a service, or something else. You will always have access to the application context.我会得到相同的应用程序上下文吗?
是的。你可以查看android文档,他们提供了
返回当前进程的单个全局Application对象的上下文。
所以对于整个应用程序进程来说不应该改变它。
另请注意这一点:
getApplicationContext()
通常仅当您需要一个生命周期与当前上下文分离的上下文(与当前上下文的生命周期相关)时才应使用进程而不是当前组件。如果我错了,请纠正我。
谢谢
Will I get the same application context?
Yes. You can check the android documentation, they have provided
Return the context of the single, global Application object of the current process.
So it should not be changed for the whole application process.
Please also take a note of this:
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.Correct me if I'm wrong.
Thanks
只有一个应用程序上下文,因此您应该获得相同的一个。您可以只使用一个带有
Context
的构造函数,实际上并不需要两个。或者,如果您想确保获取应用程序上下文,而不是活动上下文,您可以让构造函数将Application
作为参数,该参数是Context.
There is only one application context, so you should get the same one. You can have just one constructor that takes a
Context
, you don't really need two. Or if you wanted to make sure that you are getting the application context, and not, say, an activity one, you can have your constructor takeApplication
as a parameter which is aContext
.如果您想获取整个应用程序的上下文,可以使用
getApplicationContext()
。如果您想获取当前类的上下文,可以使用 getBaseContext() 代替。You can go for
getApplicationContext()
if you wanna get context of whole application. If you want to get context of current class you can usegetBaseContext()
instead.我用非静态直接上下文引用改编了 yuku 的答案。
创建一个类
domain.company.pseudo.ApplicationName
,它扩展android.app.Application
。在此示例中,我的完整应用程序包名称是
hypersoft.systems.android.starbox
。现在,修改您的 AndroidManifest.xml
标记以具有 attributeandroid:name="hypersoft.systems.android.Starbox"
,并确保Starbox.java
类文件位于项目组件目录:android
而不是starbox
。完成所有这些后,您现在可以
导入 hypersoft.systems.android.Starbox
,并且在代码中您可以通过调用Starbox.instance.getApplicationContext 获取
ApplicationContext
()使用构建工具 26 和 api 26 (Android 8.0) 以及 min sdk 版本 14 (4.0) 成功编译。
I have adapted yuku's answer with a non static direct context reference.
Create a class
domain.company.pseudo.ApplicationName
which extendsandroid.app.Application
.In this sample, my full application package name is
hypersoft.systems.android.starbox
.Now, modify your AndroidManifest.xml
<application>
tag to have the attributeandroid:name="hypersoft.systems.android.Starbox"
, and be sure theStarbox.java
class file is located in the project component directory:android
rather thanstarbox
.With all this done, you can now
import hypersoft.systems.android.Starbox
, and in your code you can get theApplicationContext
by callingStarbox.instance.getApplicationContext()
Successfully compiling with build tools 26 and api 26 (Android 8.0) with min sdk version 14 (4.0).
应用程序上下文和活动上下文都是不同的。向下转换是有风险的。使用此代码来使用上下文对象。
在您的活动和片段类中:
Conetext context=App.context;
Application Context add Activity Context both are different.Downcasting is risky .Use this code to use context object .
In Your Activities and in fragments Class :
Conetext context=App.context;