[英]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.