How the ProgressValue
(passing in the onPostExecute() in AsyncTask
) represents. it's value depends on what ?

- 63,550
- 98
- 229
- 344
2 Answers
Do you mean onProgressUpdate()
?
The progress value is anything you want. If you want to display a percentage-complete progress bar, you can use integers in the range 0..100. If you want to display a textual message, then pass a String.
At certain points in your background operation, call publishProgress()
with whatever value you want to send (integer, String, etc.). This will be passed into your onProgressUpdate()
method on the main thread, so you can display the value in your UI, if you want.
If you know how much work to do, and how far through the operation you are (perhaps your background operation runs in a loop), then on each iteration of the loop you could publish the progress as percentage of how much work is left to do.
If the background task contains a bunch of distinct operations, then maybe you want to send a message like "Loading data", "Preparing report", or something.
It's entirely up to you to decide what values to send, and how to calculate them.

- 60,055
- 21
- 138
- 179
-
How pass the value and how ? . and how a may control the progress values to cover the length of background process ? – Adham Apr 23 '12 at 10:29
When you create your AsyncTask class, you may specify types of the parameters, progress and result:
private class MyTask extends AsyncTask<ParamType, ProgressType, ResultType> { ... }
If you want to use progress values to update the progress bar, I'd recommend to use Integer, so your class declaration will look like this:
private class MyTask extends AsyncTask<ParamType, Integer, ResultType> { ... }
the following call should be made somewhere from doInBackground()
:
publishProgress(progress_value); // progress_valus is integer
and your onProgressUpdate()
member will look like this:
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
dialog.setProgress(values[0]); // dialog is the ProgressDialog
}

- 23,228
- 4
- 34
- 43