让一个应用程序上下文到Android中的静态方法的最佳方法方法、上下文、静态、应用程序

2023-09-05 05:37:55 作者:甜甜的少女心

我工作的Andr​​oid应用程序有几个活动。在这里面我有一个类有几个静态方法。我想能够调用从不同的活动,这些方法。我使用的是静态方法来通过一个XmlResourceParser一个XML文件中加载数据。要创建一个XmlResourceParser需要对应用程序上下文的电话。所以我的问题是,什么是得到一个参考应用程序上下文到静态方法的最佳方法是什么?让每个活动得到它,并通过它?在全局变量某种方式存储呢?

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?

推荐答案

更​​好的方法是将活动对象作为参数传递给静态的功能。

The better way would be to pass the Activity object as parameter to the static functions.

AFAIK,有它会给你的静态方法的应用程序上下文中没有这样的方法。

AFAIK, there is no such method which will give you the application context in the static method.