onProgressUpdate执行 [英] onProgressUpdate execution
问题描述
我在练的工作的的AsyncTask
并为此我想使用 onProgressUpdate
功能。目前在我的节目,我有一个让用户选择的输入(这将是展示在的TextView
在的AsyncTask
已完成)和定时器(决定 Thread.sleep()方法在
时间的AsyncTask
)
I am practicing the working of the AsyncTask
and with that purpose I wanted to use the onProgressUpdate
function. Currently in my program I have UI that lets the user choose an input (which would be show in a TextView
after the AsyncTask
is finished) and a timer (determines Thread.sleep()
time in the AsyncTask
)
我想要做的是...如果用户在每过第二个选择是5的时间,然后我想将通知发送到UI(其中进度对话框被称为)...指示进度 1的5 ... 5 ... 2 5 3
What I want to do is ... if the user selects a time of 5. Then in every passing second I would like to send a notification to the UI (where a progress dialog is called) ... indicating progress of 1 of 5 ... 2 of 5 ... 3 of 5.
下面是我到目前为止所取得的进展:
Here's the progress that I have made so far:
public class ViewFiller extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... input) {
// TODO Auto-generated method stub
try {
int time;
if (input[1].equalsIgnoreCase("")) {
time = 0;
} else {
time = Integer.parseInt(input[1]) * 1000;
for (int i = 1; i <= time / 1000; i++) {
publishProgress(i, time);
}
Thread.sleep(time);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return input[0];
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
execute.setText("Execute");
progress.dismiss();
tvInput.setText(result);
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
execute.setText("Running...");
displayProgress("Updating field ... ");
// Start the progress dialog
progress.show();
}
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
progress.setMessage("Updating field ... " + values[0] + " of "
+ values[1] / 1000);
}
}
目前实现只给了我 5
直接。任何人都可以建议我一个解决方案吗?
The current implementation only gives me 5 of 5
directly. Can anyone suggest me a solution ?
推荐答案
由于它会通过你的循环
比你快看看吧。
Because it is going through your loop
faster than you see it
for (int i = 1; i <= time / 1000; i++) {
publishProgress(i, time);
您不需要循环
那里。刚睡眠
不论什么时间量然后显示你的进步,并有睡眠()
和 publishProgress()
在循环
。类似
You don't need the loop
there. Just sleep
for whatever amount of time then show your progress and have the sleep()
and the publishProgress()
in the loop
. Something like
try {
int time = 0;
for (int i = 1; i <= time / 1000; i++) {
if (input[1].equalsIgnoreCase("")) {
time = 0;
} else {
time = Integer.parseInt(input[1]) * 1000;
publishProgress(time);
}
Thread.sleep(time);
}
虽然,我不知道是什么输入
实际上包含但你可能想输入[I]
。它看起来像它总是会以其他方式相同。
Although, I'm not sure what input
actually contains but you might want input[i]
. It looks like its always going to be the same otherwise.
此外, CountDownTimer 将有利于这一点,我想。
Also, CountDownTimer would be good for this I would think.
这篇关于onProgressUpdate执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!