A Handler is an Android class you can use to schedule code that should be run at some point in the future. You can also use it to post code that needs to run on a different thread.
To use the Handler, you wrap the code you wish to schedule in a Runnable object, and then use the Handler post() and postDelayed() methods to specify when you want the code to run.
post()
The post() method posts code that needs to be run as soon as possible (which is usually almost immediately). The post() method takes one parameter, an object of type ‘Runnable’. A ‘runnable’ is simply a job you want to run, just like in Java. You put the code you want to run in the Runnable’s run() method, and the Handler will make sure the code is run as soon as possible. E.g:
final Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
//code to be executed is put here
}
postDelayed()
The postDelayed() method works in a similar way to the post() method except that you use it post code that should be run in the future. The postDelayed() method takes two parameters: a Runnable and a long. The Runnable contains the code you want to run in its run() method, and the long specifies the number of milliseconds you wish to delay the code by. The code will run as soon as possible after the delay. E.g:
handler.postDelayed(Runnable, long);//long is milliseconds delay before code is executed