获得上下文的AsyncTask [英] getting context in AsyncTask
本文介绍了获得上下文的AsyncTask的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想获得的背景下我叫Opciones类的AsyncTask的(这个类是调用的任务只有一个),但我不知道该怎么做,我看到一些code这样的
I am trying to get the context in my AsyncTask of the class called Opciones(this class is the only one that call that task) but I don't know how to do it, I saw some code like this:
protected void onPostExecute(Long result) {
Toast.makeText(Opciones.this,"Subiendo la foto. ¡Tras ser moderada empezara a ser votada!: ", Toast.LENGTH_LONG).show();
}
但它并不适合我这样说的:
推荐答案
您需要做以下的事情。
- 当你想使用的AsyncTask ,拓展在其他类说 MyCustomTask
- 在新类的构造函数,通过上下文
- when you want to use AsyncTask, extend that in other class say MyCustomTask.
- in constructor of new class, pass Context
示例
public class MyCustomTask extends AsyncTask<Void, Void, Long> {
private Context mContext;
public MyCustomTask (Context context){
mContext = context;
}
//other methods like onPreExecute etc.
protected void onPostExecute(Long result) {
Toast.makeText(mContext,"Subiendo la foto. ¡Tras ser moderada empezara a ser votada!: ", Toast.LENGTH_LONG).show();
}
}
和按照实例类。
MyCustomTask task = new MyCustomTask(context);
task.execute(..);
这篇关于获得上下文的AsyncTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文