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.
Asked
Active
Viewed 621 times
2 Answers
4
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.

Psypher
- 10,717
- 12
- 59
- 83