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.
Before diving deep inside the Handler we must study about the Runnable.
A Runnable is a job you want to run.The code that we want to run in Runnable is put inside the run() method of runnable.
Handler Methods:
1) post() method:
This method posts code that need to be run ASAP(almost immediately).This methid takes only one parameter,an object of type Runnable.
final Handler handler=new Handler();
handler.post(Runnable);
2)postDelayed() method:
This method does its work in a similar way to post() method except we use it to post the code that should be run in future not immediately.
This method takes two parameter.One is Runnable object and other is delay by which you wish to delay the code in milliseconds(long datatype).The code run ASAP after delay.
final Handler handler=new Handler();
handler.postDelayed(Runnable,long);

Some code samples would've been a great help! Thanks 😁
ReplyDeleteIt's really helpful keep it up.
ReplyDelete