How to simulate Download Progress

Hi there I just want to share how can simulate in Android download progress.
With this code you can test your Progress Bars for proper work.
We have to methods startProgress with the view that we want to update for progress usually this is Progress Bar, and second method is holdForSimulation()
public void startProgress(final DownloadButton button) {
// do something long
Runnable runnable = new Runnable() {
@Override
public void run() {
for (int i = 0; i <= 10; i++) {
final int value = i;
holdForSimulation();
button.post(new Runnable() {
@Override
public void run() {
button.setProgress(value);
}
});
}
}
};
new Thread(runnable).start();
}
// Simulating download
private void holdForSimulation() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Just past your view to startProgress and wait for update.

Leave a Reply

Your email address will not be published. Required fields are marked *