Android - Delay method call

624 views Asked by At

What is the best way to delay a method call without freezing the UI or running of the program? I want to display circles on the screen every 5 seconds but in those 5 seconds, other existing circles will be changing size so the drawcircle method has to be called every 5 seconds but other code has to be able to run too.

2

There are 2 answers

0
Psypher On BEST ANSWER

Use Handler for it:

Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    //any delayed code
                }
            }, 5000);

Causes the Runnable to be added to the message queue, to be run after the specified amount of time elapses. The runnable will be run on the thread to which this handler is attached. The time-base is in milliseconds, in eg above it is 5000 milliseconds.

The postDelayed takes two parameters:

  • The Runnable that will be executed.
  • The delay (in milliseconds) until the Runnable will be executed.
0
waqaslam On

Use Handler's method called postDelayed.

For more info, read this.