如何将上下文传递给 AsyncTask?

发布于 2024-11-29 18:57:03 字数 1169 浏览 0 评论 0原文

我想在后台任务完成时执行 Toast ,只是为了让用户知道它已完成。

我为 asyncTask 创建了一个新类,但无法在该类中使用 getApplicationContext()

我正在使用 task.execute(getTempFile(this), getApplicationContext()); 来运行任务。 getTempFile 返回一个 File 对象,我试图将上下文作为 Context 对象传递。

我的 Task 类具有三个变量,AsyncTask,因此上下文位于第二个对象中。但是,这会使应用程序崩溃。

public class LocationActivity extends Activity implements LocationListener {
    protected void handleImage(Bitmap thumbnail) {
        PushDataToServer task = new PushDataToServer();
        task.execute(getTempFile(this), getApplicationContext());
    }
}


public class PushDataToServer extends AsyncTask<Object, Integer, Integer> {

    Context context;

    @Override
    protected Integer doInBackground(Object... params) {
        // TODO Auto-generated method stub
        this.context = (Context) params[1];
        File file = (File) params[0];
        return null;
    }

    protected void onPostExecute(String result) {
         Toast toast = Toast.makeText(this.context, "All done!", Toast.LENGTH_SHORT);
         toast.show();
    }

}

I want to preform a Toast when a background task is completed, just to let the user know that it's finished.

I've made a new class for my asyncTask, but I cannot use getApplicationContext() within this class.

I'm using task.execute(getTempFile(this), getApplicationContext()); to run the tasks. getTempFile returns a File object, and I was trying to pass the context as a Context object.

My Task class has three variables, AsyncTask<Object, Integer, Integer>, so the context is in the second object. However, this crashes the application.

public class LocationActivity extends Activity implements LocationListener {
    protected void handleImage(Bitmap thumbnail) {
        PushDataToServer task = new PushDataToServer();
        task.execute(getTempFile(this), getApplicationContext());
    }
}


public class PushDataToServer extends AsyncTask<Object, Integer, Integer> {

    Context context;

    @Override
    protected Integer doInBackground(Object... params) {
        // TODO Auto-generated method stub
        this.context = (Context) params[1];
        File file = (File) params[0];
        return null;
    }

    protected void onPostExecute(String result) {
         Toast toast = Toast.makeText(this.context, "All done!", Toast.LENGTH_SHORT);
         toast.show();
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

沐歌 2024-12-06 18:57:03

Context 对象传递到 AsyncTask 的构造函数中。

示例代码:

public class MyTask extends AsyncTask<?, ? ,?> {
    private Context mContext;

    public MyTask(Context context) {
        mContext = context;
    } 
}

然后,当您构建 AsyncTask 时:

MyTask task = new MyTask(this);
task.execute(...);

Pass a Context object into the AsyncTask's constructor.

Sample code:

public class MyTask extends AsyncTask<?, ? ,?> {
    private Context mContext;

    public MyTask(Context context) {
        mContext = context;
    } 
}

and then, when you are constructing your AsyncTask:

MyTask task = new MyTask(this);
task.execute(...);
长亭外,古道边 2024-12-06 18:57:03

将其传递到构造函数中,而不是作为方法参数。那么您就不需要依赖通用参数。

Pass it in the constructor, not as a method parameter. Then you don't need to depend on the generic parameters.

傲世九天 2024-12-06 18:57:03

完整示例: 可重用 AsyncTask

Complete example: Reusable AsyncTask

赠我空喜 2024-12-06 18:57:03

您说您的上下文位于第二个对象中,但您的第二个对象是 Integer。这可能是你的问题吗?另外 - 另一个建议是将您的 AsyncTask 类作为私有内部类放入您的活动中 - 这样我很确定您将有权访问 getApplicationContext()。

You say your context is in the second object, yet your second object is Integer. Could this be your problem? Also - another suggestion is to put your AsyncTask class as a private inner class to your activity - that way I am pretty sure you will have access to getApplicationContext().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文