Java 从非活动单例类获取应用程序上下文
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21818905/
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
Get application context from non activity singleton class
提问by Dmitry
In my android project, I have ImageAdapter class in which I pass app context for some further needs.
在我的 android 项目中,我有 ImageAdapter 类,我在其中传递应用程序上下文以满足进一步的需求。
public class ImageAdapter extends BaseAdapter {
private Context c;
public ImageAdapter(Context c) {
this.c = c;
}
...
}
The problem is that I wanna make ImageAdapter as a singleton to have an easy access to the instance of this class from all of my activities. But I have no idea how to pass app context from getApplicationContext() method from one of my activities to ImageAdapter. So is there any "magic" to do that as follows?
问题是我想让 ImageAdapter 成为一个单例,以便从我的所有活动中轻松访问此类的实例。但我不知道如何将应用上下文从 getApplicationContext() 方法从我的一项活动传递到 ImageAdapter。那么有没有什么“魔法”可以做到这一点,如下所示?
public class ImageAdapter extends BaseAdapter {
private Context c;
private static class Holder {
public static final ImageAdapter IA = new ImageAdapter();
}
private ImageAdapter() {
this.c = /* some magic here */.getApplicationContext();
}
public static ImageAdapter getInstance() {
return Holder.IA;
}
...
}
Maybe you have some other ideas for sharing ImageAdapter for any of my activities. I'm a newbie to android and I'm a little bit confused with the ways of passing data among activities.
也许您有一些其他想法可以为我的任何活动共享 ImageAdapter。我是 android 的新手,我对在 Activity 之间传递数据的方式有点困惑。
I will be grateful for any help.
我将不胜感激任何帮助。
采纳答案by mohammed momn
Update: 06-Mar-18
更新:18 年 3 月 6 日
Use MyApplication
instance instead of Context
instance. Application
instance is a singleton context instance itself.
使用MyApplication
实例而不是Context
实例。Application
实例本身是一个单例上下文实例。
public class MyApplication extends Application {
private static MyApplication mContext;
@Override
public void onCreate() {
super.onCreate();
mContext = this;
}
public static MyApplication getContext() {
return mContext;
}
}
Previous Answer
上一个答案
You can get the the application context like this:
您可以像这样获取应用程序上下文:
public class MyApplication extends Application {
private static Context mContext;
@Override
public void onCreate() {
super.onCreate();
mContext = getApplicationContext();
}
public static Context getContext() {
return mContext;
}
}
Then, you can call the application context from the method MyApplication.getContext()
然后,您可以从方法调用应用程序上下文 MyApplication.getContext()
Don't forget to declare the application in your manifest file:
不要忘记在清单文件中声明应用程序:
<application
android:name=".MyApplication"
android:icon="@drawable/icon"
android:label="@string/app_name" >
回答by Micik
I'd rather pass a context instance as a parameter to every method in singleton which really needs it
我宁愿将上下文实例作为参数传递给真正需要它的单例中的每个方法
回答by ToolmakerSteve
APPROACH #1:
方法#1:
Since you specify that ImageAdapter is a singleton, one simple answer is to create that singleton from a class that has access to app context:
由于您指定 ImageAdapter 是单例,一个简单的答案是从可以访问应用程序上下文的类创建该单例:
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
ImageAdapter.createIt(this);
}
}
public class ImageAdapter extends BaseAdapter {
private static ImageAdapter it;
// Get the singleton.
public static ImageAdapter getIt() {
return it;
}
// Call this once, to create the singleton.
public static void createIt(Context context) {
it = new ImageAdapter(context);
}
private final Context c;
private ImageAdapter(Context context) {
c = context;
}
}
APPROACH #2:
方法#2:
If it were not a singleton, then I would use the accepted answer. In that case, remove the local variable from ImageAdapter, because context can always be obtained from MyApplication. Expanding on the accepted answer, if you want a local method as a convenience, define ImageAdapter.getContext(). Complete solution:
如果它不是单身人士,那么我将使用已接受的答案。在这种情况下,请从 ImageAdapter 中删除局部变量,因为始终可以从 MyApplication 获取上下文。扩展已接受的答案,如果您想要一个方便的本地方法,请定义 ImageAdapter.getContext()。完整的解决方案:
public class MyApplication extends Application {
private static Context appContext;
public static Context getContext() {
return appContext;
}
@Override
public void onCreate() {
super.onCreate();
appContext = this;
}
}
public class ImageAdapter extends BaseAdapter {
public ImageAdapter() {
}
// [Optional] Call this whenever you want the app context.
private Context getContext() {
return MyApplication.getContext();
}
}