What I was trying to do was to decrease the timer delay every time the counter becomes a multiple of 5. But, as soon as the code entered the if block, it stopped incrementing the timer. I can't understand what is happening.
This is the code
thread=new Thread(){
public void run(){
try{
if(count%5==0)
timre--;
else{
//do nothing
}
//*******PROGRESS UPDATE********//
for(t=0;t<=100;t++) {
sleep((timre) / 100);
progress.setProgress(t);
t += 1;
}
//****PROGRESS UPDATE OVER****//
} catch (InterruptedException e) {
e.printStackTrace();
}
finally {
finish();
}
}//run ends
};//thread ends
thread.start();//timer starts
Thread.sleep() is not guaranteed. This means that it may or may not sleep for the duration you desire for various reasons that are out of topic for this question.
If you search the net for "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 could check them out, however, I would not use those as they provide a lot of other functionalities as the name suggest ("ScheduledThreadPoolExecutor"). You don't need this as this is most likely to be used for big systems with lots of threads etc...
If I understand your code correctly you are trying to update a progress bar. For what you are trying to do I would suggest using a handler. One of the main uses of a handler is to schedule messages and runnables to be executed as some point in the future as specified in the docs here: http://developer.android.com/reference/android/os/Handler.html
I would do it like this:
One more thing. In the line where you have this code:
Be careful when calling UI elements from other threads it is the source of lot of headache. If your handler is in another thread you should wrap that call in a function and make sure it is called from the main thread (i.e. the UI thread). Many ways to achieve that (not always necessary thou). One of them is like this: