如何从AsyncTask onPostExecute显示Toast?

时间:2015-09-09 17:24:08

标签: java android toast

当我的AsyncTask完成后,我会在TextView显示信息。

    @Override protected void onPostExecute(Void x)
    {
      MainActivity.lblLastLetterProcessed.setText("A-Z loaded");
    }

像这样:

  public static void setLastLetterProcessed(char c){
    lblLastLetterProcessed.setText("" + c);
  }

我宁愿通过Toast提供信息,但Toast需要Context

protected void onPostUpdate(Integer... progress) 
{
  Toast toast= Toast.makeText(???????????, "Loaded A-Z", Toast.LENGTH_LONG);
  toast.show();
}

所以我写了popupMessage

  void popupMessage(String text)
  {
    SpannableStringBuilder altText = new SpannableStringBuilder(text);
    altText.setSpan(new ForegroundColorSpan(0xFFCC5500), 0, altText.length(), 0);
    int duration = Toast.LENGTH_SHORT;
    Context context = getApplicationContext();
    Toast toast = Toast.makeText(context, altText, duration);
    toast.setGravity(Gravity.TOP | Gravity.CENTER, 0, 0);
    toast.show();
  }

但我无法从onPostUpdate拨打电话,因为popupMessage不是static

如果我static,我就无法使用getApplicationContext。如果我添加Context作为参数,则无法从Context传递onPostUpdate

谷歌搜索没有提供我可以实施的答案。

我该怎么办?

3 个答案:

答案 0 :(得分:3)

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

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

    ...
    ... 

    protected void onPostUpdate(Integer... progress){
        Toast toast= Toast.makeText(mContext, "Loaded A-Z", Toast.LENGTH_LONG);
        toast.show();
    }
}

并像这样实例化:

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

这就是我需要参数的AsyncTask的方法,我还在Tasks的构造函数中以这种方式定义回调,以便与调用它们的活动进行交互。

答案 1 :(得分:0)

感谢@Lucas Crawford,这有效:

public class ListMaker extends ArrayList<String> 
{
  Context mContext; //////////////////////////////
  ...    

  public ListMaker(AssetManager assets, Context c){
    //                                  ^^^^^^^^^^
    ...    
    mContext = c;  /////////////////////////////////

    LoadWords loadWords = new LoadWords();
    ...
    loadWords.execute((Object []) null);
  }

  private class LoadWords extends AsyncTask<Object, Integer, Void>
  {
    @Override protected Void doInBackground(Object... params) 
    {
       ...
       publishProgress(...);
    }

    @Override protected void onPostExecute(Void x)
    {
      Toast toast= Toast.makeText(mContext, "Loaded A-Z", Toast.LENGTH_LONG);
       //                         ^^^^^^^^
      toast.show();

    }
    ...
  }
}

更好的是我有一个改进的,更实用的popupMessage我可以从onPostExecute调用,如下所示:

     MainActivity.popupMessage("Loaded A-Z", mContext);

这就是:

  public static void popupMessage(String text, Context context)
  //     ******                                **********
  {
    SpannableStringBuilder altText = new SpannableStringBuilder(text);
    altText.setSpan(new ForegroundColorSpan(0xFFCC5500), 0, altText.length(), 0);
    int duration = Toast.LENGTH_SHORT;
    Toast toast = Toast.makeText(context, altText, duration);
    toast.setGravity(Gravity.TOP | Gravity.CENTER, 0, 0);
    toast.show();
  }

答案 2 :(得分:0)

我希望你在一个单独的类中有异步方法

package nl.yentel.finekinney;

import android.app.Activity;
import android.os.AsyncTask;
import android.view.Gravity;
import android.widget.Toast;

public class Demo extends AsyncTask<String, Void, String> {
     Activity demoActivity; //this is a public variable

public void ActivityName(Activity activity) {
    //calling this void will set the variable demoActivity to the input activity
    demoActivity = activity;
}

@Override
protected String doInBackground(String... strings) {
    return null;
}

@Override
protected void onPreExecute() {
}

@Override
protected void onPostExecute(String s) {
    Toast toast = Toast.makeText(demoActivity, "the text in the toast", Toast.LENGTH_LONG);
    toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 0);
    toast.show();
}
}

那我必须调用这个类

public void openMail(final View view) {
    //first create a pointer to the Demo Class
    Demo JsonConnect = new Demo();
    //then attach the context activity to the ActivityName

    //in the Class 'demoActivity' will point to the calling activity;
    JsonConnect.ActivityName(this);
    JsonConnect.execute();
}

现在 toast 将指向正确的调用 Activity

Toast toast = Toast.makeText(demoActivity, "the text in the toast", Toast.LENGTH_LONG);