获取应用程序上下文

发布于 2024-11-30 18:00:39 字数 268 浏览 1 评论 0原文

这可能是一个简单的问题,但我只是想确保我是对的。

在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

回忆追雨的时光 2024-12-07 18:00:39

获取应用程序上下文的最简单方法是:

创建一个扩展 android.app.Application 的类 App

public class App extends Application {
    public static Context context;

    @Override public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
    }
}

修改 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 extends android.app.Application

public class App extends Application {
    public static Context context;

    @Override public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
    }
}

Modify your AndroidManifest.xml 's <application> tag to have the attribute android: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.

夜空下最亮的亮点 2024-12-07 18:00:39

我会得到相同的应用程序上下文吗?

是的。你可以查看android文档,他们提供了

 getApplicationContext()

返回当前进程的单个全局Application对象的上下文。

所以对于整个应用程序进程来说不应该改变它。

另请注意这一点:

getApplicationContext() 通常仅当您需要一个生命周期与当前上下文分离的上下文(与当前上下文的生命周期相关)时才应使用进程而不是当前组件。

如果我错了,请纠正我。

谢谢

Will I get the same application context?

Yes. You can check the android documentation, they have provided

 getApplicationContext()

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

橘亓 2024-12-07 18:00:39

只有一个应用程序上下文,因此您应该获得相同的一个。您可以只使用一个带有 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 take Application as a parameter which is a Context.

冷情妓 2024-12-07 18:00:39

如果您想获取整个应用程序的上下文,可以使用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 use getBaseContext() instead.

忘羡 2024-12-07 18:00:39

我用非静态直接上下文引用改编了 yuku 的答案。

创建一个类 domain.company.pseudo.ApplicationName,它扩展 android.app.Application

package hypersoft.systems.android;

import android.app.Application;

public class Starbox extends Application {

  public static Starbox instance;

  @Override
  public void onCreate() {
    super.onCreate();
    instance = this;
  }

}

在此示例中,我的完整应用程序包名称是hypersoft.systems.android.starbox

现在,修改您的 AndroidManifest.xml 标记以具有 attribute android: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 extends android.app.Application.

package hypersoft.systems.android;

import android.app.Application;

public class Starbox extends Application {

  public static Starbox instance;

  @Override
  public void onCreate() {
    super.onCreate();
    instance = this;
  }

}

In this sample, my full application package name is hypersoft.systems.android.starbox.

Now, modify your AndroidManifest.xml <application> tag to have the attribute android:name="hypersoft.systems.android.Starbox", and be sure the Starbox.java class file is located in the project component directory: android rather than starbox.

With all this done, you can now import hypersoft.systems.android.Starbox, and in your code you can get the ApplicationContext by calling Starbox.instance.getApplicationContext()

Successfully compiling with build tools 26 and api 26 (Android 8.0) with min sdk version 14 (4.0).

梦幻的心爱 2024-12-07 18:00:39

应用程序上下文和活动上下文都是不同的。向下转换是有风险的。使用此代码来使用上下文对象。

public class App extends Application {
public static Context context;

    @Override public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
    }
} 

在您的活动和片段类中:

Conetext context=App.context;

Application Context add Activity Context both are different.Downcasting is risky .Use this code to use context object .

public class App extends Application {
public static Context context;

    @Override public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
    }
} 

In Your Activities and in fragments Class :

Conetext context=App.context;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文