What is the equivalent to a JavaScript setInterval/setTimeout in Android/Java?

As always with Android there’s lots of ways to do this, but assuming you simply want to run a piece of code a little bit later on the same thread, I use this:

new android.os.Handler(Looper.getMainLooper()).postDelayed(
    new Runnable() {
        public void run() {
            Log.i("tag", "This'll run 300 milliseconds later");

.. this is pretty much equivalent to

    function() {
        console.log("This will run 300 milliseconds later");

Leave a Comment