Asynctask获得java进度的百分比

时间:2016-10-21 17:54:55

标签: java android android-asynctask percentage

我有AsyncTask解压缩文件。是否有可能获得AsyncTask的进度百分比以横向ProgressBar显示?

2 个答案:

答案 0 :(得分:1)

覆盖public class MainActivity extends BaseActivity { private long remainingTimeForTimer = 0; private CountDownTimer mCountDownTimer @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Start first timer test1(120 * 1000); // now on the basis of remaining timer you can cancel current timer and after that second timer you can start that timer with remaining time if(remainingTimeForTimer > 0) { test1(remainingTimeForTimer); } } private void test1(long totalTimerTime) { mCountDownTimer = new CountDownTimer(totalTimerTime, 1000) { @Override public void onTick(long millisUntilFinished) { remainingTimeForTimer = millisUntilFinished; } @Override public void onFinish() { //trialCount = 0; } }; mCountDownTimer.start(); } @Override protected void onDestroy() { if (mCountDownTimer != null) { mCountDownTimer.cancel(); } super.onDestroy(); } } 中的onProgressUpdate。然后从后台线程中调用AsyncTask。将使用publishProgress中的参数在UI线程上调用onProgressUpdate

使用publishProgress中的第二个类型参数确定progress参数的类型。

答案 1 :(得分:0)

AsyncTask有onProgressUpdate(Integer ...)方法,你可以通过调用publishProgress()在doInBackground()期间调用每次迭代或者每次调用进程。

有关详细信息,请参阅doc