Thread.sleep () is not guaranteed. This means that he may or may not sleep for a long time for various reasons that are not relevant to the topic for this issue.
If you are looking for a network for the “timer in android”, you will probably land on these two: https://developer.android.com/reference/java/util/Timer.html and https://developer.android.com/ reference / java / util / concurrent / ScheduledThreadPoolExecutor.html
You can test them, but I would not use them, because they provide many other functions, as the name suggests ("ScheduledThreadPoolExecutor"). You do not need this, since it will most likely be used for large systems with a large number of threads, etc.
If I understand your code correctly, you are trying to update the progress bar. For what you are trying to do, I would suggest using a handler. One of the main uses of the handler is to plan messages and executables that will be executed as some points in the future, as indicated in the documents: http://developer.android.com/reference/android/os/Handler.html
I would do it like this:
int counter = 0; int delayInMs = 5000;
One more thing. In the line where you have this code:
progress.setProgress(t);
Be careful when invoking user interface elements from other threads; this is the source of a lot of headache. If your handler is in another thread, you must transfer this call to the function and make sure that it is called from the main thread (i.e., the UI Thread). Many ways to achieve this (not always necessary). One of them is as follows:
private void updateProgress(int counter) { WhateverActivity.this.runOnUiThread(new Runnable() { public void run() { progress.setProgress(counter); } }); }
source share