如何将上下文传递给 AsyncTask?
我想在后台任务完成时执行 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将
Context
对象传递到AsyncTask
的构造函数中。示例代码:
然后,当您构建
AsyncTask
时:Pass a
Context
object into theAsyncTask
's constructor.Sample code:
and then, when you are constructing your
AsyncTask
:将其传递到构造函数中,而不是作为方法参数。那么您就不需要依赖通用参数。
Pass it in the constructor, not as a method parameter. Then you don't need to depend on the generic parameters.
完整示例: 可重用 AsyncTask
Complete example: Reusable AsyncTask
您说您的上下文位于第二个对象中,但您的第二个对象是 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().