如何显示来自 AsyncTask onPostExecute 的 Toast?

How can I show a Toast from AsyncTask onPostExecute?

当我的 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 作为参数,我无法从 onPostUpdate 传递 Context

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

我能做什么?

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 的方式,我还在任务的构造函数中以这种方式定义回调,以便与调用它们的活动进行交互。

感谢@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();
  }

我希望你在单独的 Class

中有异步方法
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();
}
}

那我得叫这个Class

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);