简体   繁体   English

如何从 AsyncTask onPostExecute 显示 Toast?

[英]How can I show a Toast from AsyncTask onPostExecute?

When my AsyncTask finishes, I display info in a TextView .当我的AsyncTask完成时,我会在TextView显示信息。

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

Like so:像这样:

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

I'd rather give the info via Toast but Toast requires Context .我宁愿通过Toast提供信息,但Toast需要Context

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

So I wrote popupMessage :所以我写了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();
  }

But I can't call it from onPostUpdate since popupMessage is NOT static .但是我不能从onPostUpdate调用它,因为popupMessage不是static

If I make it static , I can't use getApplicationContext .如果我将其getApplicationContext static ,则无法使用getApplicationContext And if I add Context as a parameter, I can't pass Context from onPostUpdate .如果我将Context添加为参数,则无法从onPostUpdate传递Context

Googling has provided no SO Answers I can implement.谷歌搜索没有提供我可以实施的 SO 答案。

What can I do?我能做什么?

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

and instantiate it like so:并像这样实例化它:

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

This is how I do AsyncTask's that require arguments, I also define callbacks this way in the constructor of the Tasks to interact with the activities that call them.这就是我执行需要参数的 AsyncTask 的方式,我还在任务的构造函数中以这种方式定义回调,以与调用它们的活动进行交互。

Thanks to @Lucas Crawford, this works:感谢@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();

    }
    ...
  }
}

And even better I have an improved, more useful popupMessage that I can call from onPostExecute like so:更好的是,我有一个改进的、更有用的popupMessage ,我可以像这样从onPostExecute调用onPostExecute

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

And here it is:这是:

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

I expect you have the async method in a seperate 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();
}
}

Then I have to call this 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();
}

Now the toast will point to the proper calling activity现在 toast 将指向正确的调用活动

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从AsyncTask类的onPostExecute方法返回值? - How can i return value from AsyncTask class onPostExecute method? 无法在AsyncTask的onPostExecute方法上显示Toast或AlertDialog - Cannot show Toast nor AlertDialog on AsyncTask's onPostExecute method 我如何在asynctask中创建arrayadapter。 onPostExecute - how can i create an arrayadapter in asynctask. onPostExecute 如何存储来自 OnPostExecute() 的数据? - How can I store data from OnPostExecute()? 在AsyncTask中显示Toast - Show Toast inside AsyncTask 我怎样才能将OnPostExecute()的结果导入主活动,因为AsyncTask是一个单独的类? - How can i to get the result of OnPostExecute() to main activity because AsyncTask is a separate class? 如果我有一个单独的类扩展了一个如何从onPostExecute()获取值 <AsyncTask> 类? - How to get a value from onPostExecute() to if I have a separate class that extends an <AsyncTask> class? 如何从内部AsyncTask类的onPostExecute方法修改外部字段? - How to modify external field from onPostExecute method of an inner AsyncTask class? 如何从doInBackground()方法返回JSONObject到AsyncTask上的onPostExecute()方法? - How to return JSONObject from doInBackground() method to onPostExecute() method on AsyncTask? 在Android中:如何将OnPostExecute()的结果发送到其他活动? - In Android: How can i send the result of from OnPostExecute() to other activity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM