I had to add Looper to the following code:
public class MyRunnable implements Runnable { @Override public void run() { Looper.prepare(); final Looper looper = Looper.myLooper(); new Handler().postDelayed( new Runnable() { @Override public void run() { try { } catch (Exception ex) { } finally { looper.quit(); } } }, 100); Looper.loop(); } }
Please note that I have runnable inside runnable. A nested runnable is executed through a handler. I initially did not have Looper, but Android complained that I needed to call Looper.prepare before executing another thread.
I read Looper, but it still seems cryptic. It seems to act like some kind of internal message pipeline. I do not understand why this is necessary, since there are no messages going from my external runnable to my internal runnable. Although true, it seems that Android is just making a tough rule: if you call a thread from a thread, you MUST also call Looper.prepare. Even if I take it as it is, it still doesn't help to understand why I need to call looper.loop and looper.quit. If I omit Looper.loop, my handler never starts, and this is not clear. What does Looper.loop do to make my handler work?
source share