0

I want to run some code for 20 seconds precisely. It is similar to a loop but instead of having a variable I have time (in seconds).

I should have a time condition like this:

do

{ variable++ }

while (sec < 20)

How it is possible to do this in Android??

My application should run this 20 sec code after the user presses a button.

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
Ricardo
  • 121
  • 1
  • 2
  • 11

1 Answers1

1

You can use the Handler class in Android on a runnable and then use the postDelayed() method. That way you will be able to update the UI during that 20 seconds on the progress of the thread. A good example of this is hear. Your code might look something like this ...

Handler handler = new Handler();
final Runnable r = new Runnable(){
    public void run() {
        //Do thing after 20 sec        
    }
};

handler.postDelayed(r, 20000);
Community
  • 1
  • 1
Stefan Bossbaly
  • 6,682
  • 9
  • 53
  • 82