Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dmpatel151282/2bb9637dc94c1fcf416026704d8094eb to your computer and use it in GitHub Desktop.
Save dmpatel151282/2bb9637dc94c1fcf416026704d8094eb to your computer and use it in GitHub Desktop.
There are three ways to achieve this in Android.
Through Worker thread:-
private static final ScheduledExecutorService worker =
Executors.newSingleThreadScheduledExecutor();
void someMethod() {
...
Runnable task = new Runnable() {
public void run() {
/* Do something... */
}
};
worker.schedule(task, 5, TimeUnit.SECONDS);
...
}
Through Timer:-
new Timer().schedule(new TimerTask() {
@Override
public void run() {
// This code will be executed after 2 seconds
}
}, 2000);
Through Handler :-
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
//Do something after 100ms
}
}, 100);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment