在 Android 中将应用程序上下文转换为静态方法的最佳方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2785670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best way to get an Application Context into a static method in Android
提问by Slapout
I'm working on an Android application that has several Activities. In it I have a class with several static methods. I would like to be able to call these methods from the different Activities. I'm using the static methods to load data from an xml file via a XmlResourceParser. To create a XmlResourceParser requires a call on the Application Context. So my question is, what is the best way to get a reference to the Application Context into the static methods? Have each Activity get it and pass it in? Store it somehow in a global variable?
我正在开发具有多个活动的 Android 应用程序。其中我有一个包含几个静态方法的类。我希望能够从不同的活动中调用这些方法。我正在使用静态方法通过 XmlResourceParser 从 xml 文件加载数据。创建 XmlResourceParser 需要调用应用程序上下文。所以我的问题是,将应用程序上下文引用到静态方法中的最佳方法是什么?是否让每个 Activity 获取并传入?以某种方式将它存储在全局变量中?
采纳答案by Karan
The better way would be to pass the Activity object as parameter to the static functions.
更好的方法是将 Activity 对象作为参数传递给静态函数。
AFAIK, there is no such method which will give you the application context in the static method.
AFAIK,没有这样的方法可以在静态方法中为您提供应用程序上下文。
回答by gjpc
I am not sure this is going to work all the time but it works for me now:
我不确定这是否会一直有效,但现在对我有用:
public class myActivity extends ListActivity
{
public static Context baseContext;
public void onCreate(Bundle savedInstanceState)
{
baseContext = getBaseContext();
}
Then you may use the static in your package:
然后你可以在你的包中使用静态:
myApplication.baseContext
回答by Versa
This should get you access to applicationContext
from anywhere allowing you to get applicationContext
anywhere that can use it; Toast
, getString()
, sharedPreferences
, etc. I have used this to get applicationContext
inside of static methods multiple times.
这应该让您可以applicationContext
从任何地方访问,从而可以到达applicationContext
任何可以使用它的地方;Toast
、getString()
、sharedPreferences
等。我多次使用它来获取applicationContext
静态方法的内部。
The Singleton:
单身人士:
package com.domain.packagename;
import android.content.Context;
/**
* Created by Versa on 10.09.15.
*/
public class ApplicationContextSingleton {
private static PrefsContextSingleton mInstance;
private Context context;
public static ApplicationContextSingleton getInstance() {
if (mInstance == null) mInstance = getSync();
return mInstance;
}
private static synchronized ApplicationContextSingleton getSync() {
if (mInstance == null) mInstance = new PrefsContextSingleton();
return mInstance;
}
public void initialize(Context context) {
this.context = context;
}
public Context getApplicationContext() {
return context;
}
}
Initialize the Singleton in your Application
subclass:
在您的Application
子类中初始化单例:
package com.domain.packagename;
import android.app.Application;
/**
* Created by Versa on 25.08.15.
*/
public class mApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
ApplicationContextSingleton.getInstance().initialize(this);
}
}
If I′m not wrong, this gives you a hook to applicationContext everywhere, call it with ApplicationContextSingleton.getInstance.getApplicationContext();
You shouldn′t need to clear this at any point, as when application closes, this goes with it anyway.
如果我没记错的话,这会给你一个到处都是 applicationContext 的钩子,调用它ApplicationContextSingleton.getInstance.getApplicationContext();
你不应该在任何时候清除它,因为当应用程序关闭时,它无论如何都会发生。
Remember to update AndroidManifest.xml
to use this Application
subclass:
请记住更新AndroidManifest.xml
以使用此Application
子类:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.domain.packagename"
>
<application
android:allowBackup="true"
android:name=".mApplication" <!-- This is the important line -->
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:icon="@drawable/app_icon"
>
Please let me know if you see anything wrong here, thank you. :)
如果您发现这里有任何错误,请告诉我,谢谢。:)
回答by Tom Susel
There's a post in Sane Tricks For InsaneWorld blog with an answer. It says you can replace the Application object with your own subclass and then keep the app context statically there. You can find example code in the post.
Sane Tricks For InsaneWorld 博客中有一篇文章给出了答案。它说您可以用您自己的子类替换 Application 对象,然后将应用程序上下文静态地保留在那里。您可以在帖子中找到示例代码。
The original blog post - http://uquery.blogspot.co.il/2011/08/how-to-get-application-context.html
原始博客文章 - http://uquery.blogspot.co.il/2011/08/how-to-get-application-context.html