Java 如何在调用时将上下文对象传递给线程

Java 如何在调用时将上下文对象传递给线程,java,android,Java,Android,我有一段代码: public static class ExportDatabaseFileTask extends AsyncTask<String, Void, Boolean> { private final ProgressDialog dialog = new ProgressDialog(ctx); protected void onPreExecute(); protected Boolean doInBackground(final Str

我有一段代码:

public static class ExportDatabaseFileTask extends AsyncTask<String, Void, Boolean> 
{
    private final ProgressDialog dialog = new ProgressDialog(ctx);

    protected void onPreExecute();
    protected Boolean doInBackground(final String... args);
    protected void onPostExecute(final Boolean success);
}
正如您所看到的,我在newprogressdialog调用中使用ctx作为上下文变量,如何将上下文传递给call方法

对于这一点:

new ExportDatabaseFileTask().execute();*

只需定义一个静态setter方法,您可以在其中传递上下文对象

我找到了方法,我必须创建自己的构造函数,并丢失了静态内容

        public ExportDatabaseFileTask(Context ctx) {
            super();
            this.ctx=ctx;
            dialog= new ProgressDialog(ctx);
        }

我只是在开发指南中偶然发现了这一点,我相信这正是它的目的
android.content.ContextWrapper

是的,好的,也可以。。。。我认为static关键字是必需的,在静态类中不能定义构造函数。实际上,我需要在类名前面加上static关键字,否则我无法识别它,但我创建了一个新的实例。这让我烦了好几个小时。为什么在AsyncTask中有ProgressDialog?我总是把它放在我的活动中,并使用publishProgress更新它
        public ExportDatabaseFileTask(Context ctx) {
            super();
            this.ctx=ctx;
            dialog= new ProgressDialog(ctx);
        }