问题描述
我已经创建了一个异步任务,并希望在doBackground的不同阶段来改变进度对话框的消息。这里是code:
公共类SC扩展的AsyncTask<整数,字符串,太虚>
{
ProgressDialog对话框;
在preExecute保护无效()
{
对话框=新ProgressDialog(Loc.this);
dialog.show();
}
@覆盖
保护无效doInBackground(整数... PARAMS)
{ onProgressUpdate(联系server..Please等待..);
//做一些工作
onProgressUpdate(处理结果);
//做一些工作
onProgressUpdate(计算..);
dialog.dismiss();
返回null;
}
保护无效onProgressUpdate(字符串UI)
{
dialog.setMessage(UI);
}
}
但问题是,进度对话框只显示总的第一条消息。请帮我找到一个解决方案。
保护无效doInBackground(整数... PARAMS)
{
onProgessUpdate(联系server..Please等待..);
...
}
Urrrm,不,这是行不通的。
尝试...
publishProgress(联系server..Please等待..);
您必须发布在你的进步doInBackground(..)
,以便 onProgressUpdate(.. 。)
被调用。
另外,不要叫 dialog.dismiss()
在 doInBackground(...)
叫它 onPostExecute(...)
代替。
I have created an async task and want to change the message of progress dialog during different stages of doBackground. Here is the code:
public class sc extends AsyncTask<Integer,String,Void>
{
ProgressDialog dialog;
protected void onPreExecute()
{
dialog=new ProgressDialog(Loc.this);
dialog.show();
}
@Override
protected Void doInBackground(Integer... params)
{
onProgressUpdate("Contacting server..Please wait..");
//Do some work
onProgressUpdate("Processing the result");
//Do some work
onProgressUpdate("Calculating..");
dialog.dismiss();
return null;
}
protected void onProgressUpdate(String ui)
{
dialog.setMessage(ui);
}
}
But the problem is that, progress dialog is only showing the first message always. Kindly help me to find a solution.
protected Void doInBackground(Integer... params)
{
onProgessUpdate("Contacting server..Please wait..");
...
}
Urrrm, nope, that won't work.
Try...
publishProgress("Contacting server..Please wait..");
You have to "publish" your progress in doInBackground(..)
in order for onProgressUpdate(...)
to be called.
Also don't call dialog.dismiss()
in doInBackground(...)
call it in onPostExecute(...)
instead.
这篇关于难点在异步任务不断变化的进度对话框的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!