是否有一种方法来获得静态方法内的当前上下文实例?

我正在寻找这种方式,因为我讨厌保存'Context'实例每次它改变。


当前回答

如果你愿意使用RoboGuice,你可以将上下文注入到任何你想要的类中。下面是如何使用RoboGuice 2.0(在撰写本文时是beta 4)的一个小示例

import android.content.Context;
import android.os.Build;
import roboguice.inject.ContextSingleton;

import javax.inject.Inject;

@ContextSingleton
public class DataManager {
    @Inject
    public DataManager(Context context) {
            Properties properties = new Properties();
            properties.load(context.getResources().getAssets().open("data.properties"));
        } catch (IOException e) {
        }
    }
}

其他回答

Assuming we're talking about getting the Application Context, I implemented it as suggested by @Rohit Ghatol extending Application. What happened then, it's that there's no guarantee that the context retrieved in such a way will always be non-null. At the time you need it, it's usually because you want to initialize an helper, or get a resource, that you cannot delay in time; handling the null case will not help you. So I understood I was basically fighting against the Android architecture, as stated in the docs

注意:通常不需要子类化Application。在大多数情况下,静态单例可以以更模块化的方式提供相同的功能。如果你的单例需要一个全局上下文(例如注册广播接收器),在调用你的单例的getInstance()方法时,包括context . getapplicationcontext()作为context参数。

并由黛安·哈克伯恩解释

Application作为派生对象存在的唯一原因是,在1.0之前的开发过程中,我们的一个应用程序开发人员不断地纠缠我,说需要一个顶级的应用程序对象来派生,这样他们就可以有一个更“正常”的应用程序模型,我最终屈服了。 我将永远后悔在这一点上做出了让步。:)

她还提出了这个问题的解决方案:

如果你想要的是一些全局状态,可以在应用程序的不同部分之间共享,请使用单例。[…这就更自然地引出了你应该如何管理这些东西——按需初始化它们。

所以我所做的是摆脱扩展Application,并将上下文直接传递给单例helper的getInstance(),同时在私有构造函数中保存对应用程序上下文的引用:

private static MyHelper instance;
private final Context mContext;    

private MyHelper(@NonNull Context context) {
    mContext = context.getApplicationContext();
}

public static MyHelper getInstance(@NonNull Context context) {
    synchronized(MyHelper.class) {
        if (instance == null) {
            instance = new MyHelper(context);
        }
        return instance;
    }
}

调用者将传递一个本地上下文给helper:

Helper.getInstance(myCtx).doSomething();

因此,要正确地回答这个问题:有很多方法可以静态地访问应用程序上下文,但都不建议使用,您应该更喜欢将本地上下文传递给单例的getInstance()。


感兴趣的人可以在fwd博客上阅读更详细的版本

另一种获得上下文而不子类化Application对象和不使用隐藏类的方法是使用ContentProvider。一旦onCreate方法被调用,上下文应该是可用的。您可以在Kotlin中执行类似的操作

class ContextContentProvider : ContentProvider() {
    override fun delete(uri: Uri, selection: String?, selectionArgs: Array<String>?) = 0

    override fun getType(uri: Uri): String? = null

    override fun insert(uri: Uri, values: ContentValues?): Uri? = null

    override fun onCreate(): Boolean {
        applicationContext = context
        return true
    }

    override fun query(
        uri: Uri, projection: Array<String>?, selection: String?,
        selectionArgs: Array<String>?, sortOrder: String?
    ): Cursor? = null

    override fun update(
        uri: Uri, values: ContentValues?, selection: String?,
        selectionArgs: Array<String>?
    ) = 0

    companion object {
        private var applicationContext: Context? = null

        @JvmStatic
        fun applicationContext() = applicationContext
    }
}

在任何需要上下文的地方,都可以调用ContextContentProvider.applicationContext()方法

如果你已经有了另一个内容提供程序,并且没有导出该内容提供程序,请确保在AndroidManifest.xml中使用不同的权限。

<application>
    <provider
        android:name=".ContextContentProvider"
        android:authorities="${applicationId}.ContextContentProvider"
        android:enabled="true"
        android:exported="false" />
</application>

不,我想没有。不幸的是,您只能从Activity或Context的其他子类调用getApplicationContext()。而且,这个问题有点相关。

大多数想要一个方便的方法来获取应用上下文的应用程序都会创建自己的类来扩展android.app.Application。

指南

你可以先在你的项目中创建一个类,如下所示:

import android.app.Application;
import android.content.Context;

public class App extends Application {

    private static Application sApplication;

    public static Application getApplication() {
        return sApplication;
    }

    public static Context getContext() {
        return getApplication().getApplicationContext();
    }

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

然后,在你的AndroidManifest中,你应该在AndroidManifest.xml的标签中指定你的类名:

<application 
    ...
    android:name="com.example.App" >
    ...
</application>

然后你可以使用以下方法在任何静态方法中检索应用程序上下文:

public static void someMethod() {
    Context context = App.getContext();
}

警告

在向你的项目添加上述内容之前,你应该考虑一下文档中的内容:

通常不需要子类化Application。在大多数情况下, 静态单例可以以更模块化的方式提供相同的功能 道路如果你的单例需要一个全局上下文(例如注册 广播接收器),检索它的函数可以给出一个 在内部使用Context. getapplicationcontext()时 首先构造单例。


反射

还有另一种使用反射获取应用程序上下文的方法。在Android中,反射经常被轻视,我个人认为不应该在产品中使用。

要检索应用程序上下文,我们必须调用一个隐藏类(ActivityThread)的方法,该方法从API 1开始就可用:

public static Application getApplicationUsingReflection() throws Exception {
    return (Application) Class.forName("android.app.ActivityThread")
            .getMethod("currentApplication").invoke(null, (Object[]) null);
}

还有一个隐藏类(AppGlobals),它提供了一种以静态方式获取应用程序上下文的方法。它使用ActivityThread获取上下文,所以下面的方法和上面发布的方法没有区别:

public static Application getApplicationUsingReflection() throws Exception {
    return (Application) Class.forName("android.app.AppGlobals")
            .getMethod("getInitialApplication").invoke(null, (Object[]) null);
} 

编码快乐!

根据这个源代码,您可以通过扩展ContextWrapper来获得自己的Context

public class SomeClass extends ContextWrapper {

    public SomeClass(Context base) {
      super(base);
    }

    public void someMethod() {
        // notice how I can use "this" for Context
        // this works because this class has it's own Context just like an Activity or Service
        startActivity(this, SomeRealActivity.class);

        //would require context too
        File cacheDir = getCacheDir();
    }
}

JavaDoc for ContextWrapper

Context的代理实现,简单地将其所有调用委托给另一个Context。可以通过子类化来修改行为而不改变原始上下文。