I would like to create a program that execute a method after 10 seconds, how to do it in java or android???
Asked
Active
Viewed 4,815 times
0
-
Use a `Timer` or a `ScheduledExecutorService`. – Sanjay T. Sharma Jan 28 '13 at 12:24
-
1You can use Runnable & Handler combination. Example : http://stackoverflow.com/questions/8177830/hide-a-layout-after-10-seconds-in-android/8177879#8177879 – Kartik Domadiya Jan 28 '13 at 12:25
-
https://stackoverflow.com/a/9166354/7147289 use it – Guy4444 Jan 09 '19 at 10:54
5 Answers
4
You could use a ScheduledExecutorService, and submit a Runnable or Callable which calls your method.

JB Nizet
- 678,734
- 91
- 1,224
- 1,255
3
Try this code,
final Handler mTimerHandler = new Handler();
final Handler threadHandler = new Handler();
new Thread() {
@Override
public void run() {
threadHandler.postDelayed(new Runnable() {
public void run() {
}
}, 5000);
}
}.start();

MuraliGanesan
- 3,233
- 2
- 16
- 22
2
Look at TimerTask : A task that can be scheduled for one-time or repeated execution by a Timer.
Example: Schedule Periodic Tasks

Azodious
- 13,752
- 1
- 36
- 71