I am trying to use this library to publish progress on the Activity screen in the onProgressUpdate()
of an AsyncTask, which is used to download some data from the internet. The demo application they have provided is using Handler
to demonstrate this. I need to use it in an AsyncTask.
Following code snippet is from the code in their demo application.
My questions are:
onProgressUpdate gets an argument
Progress... values
, and according to the documentation, it is "the values indicating progress." But I don't understand what this means, and how to I use this argument. In the examples I have seen, they are doing something like:progressBar.setProgress(Integer.parseInt(values[0]));
Why are they using only the first value in the passed array (values[0])?
How does the
AsyncTask
know how long is it going to take to finish the download operation. I want to show the ProgressBar progress from value 0 to 100 during the time spent to finish the download operation.By doing
progressBarDeterminate.setVisibility(View.VISIBLE)
inonPreExecute
and doingprogressBarDeterminate.setVisibility(View.GONE)
inonPostExecute
, we are ensuring that it only shows when the data is being downloaded, but how to we ensure that it progresses from starting position to final position during the time it takes to download the data (when we don't know precisely how long is it going to take to download the data) ?
CODE FROM DEMO EXAMPLE (It uses Handler, but I need to do this in AsyncTask)
public class MainActivity extends AppCompatActivity {
ProgressBarDeterminate progressBarDeterminate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBarDeterminate = (ProgressBarDeterminate) findViewById(R.id.progressDeterminate);
progressTimer.start();
}
Thread progressTimer = new Thread(new Runnable() {
@Override
public void run() {
for(int i = 0; i <= 100; i++){
try {
Thread.sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.sendMessage(new Message());
}
}
});
Handler handler = new Handler(new Handler.Callback() {
int progress = 0;
@Override
public boolean handleMessage(Message msg) {
progressBarDeterminate.setProgress(progress++);
return false;
}
});
}