Created
August 20, 2016 12:37
-
-
Save dmpatel151282/2bb9637dc94c1fcf416026704d8094eb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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