Tutorial by Examples

Executing code after 1.5 seconds: Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { //The code you want to run after the time is up } }, 1500); //the time you want to delay in milliseconds Executing code repeatedly every...
As Handlers are used to send Messages and Runnables to a Thread's message queue it's easy to implement event based communication between multiple Threads. Every Thread that has a Looper is able to receive and process messages. A HandlerThread is a Thread that implements such a Looper, for example th...
To stop the Handler from execution remove the callback attached to it using the runnable running inside it: Runnable my_runnable = new Runnable() { @Override public void run() { // your code here } }; public Handler handler = new Handler(); // use 'new Handler(Looper.get...
This can be useful if you're writing a game or something that needs to execute a piece of code every a few seconds. import android.os.Handler; public class Timer { private Handler handler; private boolean paused; private int interval; private Runnable task = new Runnable ...

Page 1 of 1