I made a test app where I would like to see a ProgressBar
being updated by simulating a long-running task. I initially tried to use Handler
because after some research I saw that its use was recommended:
final int tempoDeEspera = 500;
new Handler().post(new Runnable() {
@Override
public void run() {
SystemClock.sleep(tempoDeEspera);
progressBar.setProgress(1);
SystemClock.sleep(tempoDeEspera);
progressBar.setProgress(2);
SystemClock.sleep(tempoDeEspera);
progressBar.setProgress(3);
SystemClock.sleep(tempoDeEspera);
progressBar.setProgress(4);
SystemClock.sleep(tempoDeEspera);
progressBar.setProgress(5);
}
});
But I only got the expected result using Thread
.
final int tempoDeEspera = 500;
new Thread(new Runnable() {
@Override
public void run() {
SystemClock.sleep(tempoDeEspera);
progressBar.setProgress(1);
SystemClock.sleep(tempoDeEspera);
progressBar.setProgress(2);
SystemClock.sleep(tempoDeEspera);
progressBar.setProgress(3);
SystemClock.sleep(tempoDeEspera);
progressBar.setProgress(4);
SystemClock.sleep(tempoDeEspera);
progressBar.setProgress(5);
}
}).start();
I would like to know the details of the two implementations and the situations in which each one should be used.