In Android, Main Thread & HandlerThread
has Looper & MessageQueue by default. I can call getLooper() on handlerThread object, but why not on main Thread ?
HandlerThread ht = new HandlerThread();
Looper htLooper = ht.getLooper(); // Works fine
Thread mainThread = Looper.getMainLooper().getThread();
Looper mainLooper = mainThread.getLooper(); // getLooper() doesn't compile.
In a real scenario, one would never need to use getLooper() on mainThread; we can just call Looper.getMainLooper()
. I would just like to know why it doesn't work.
I understand it from a Java perspective, that Looper.getMainLooper().getThread()
returns a java.lang.Thread
, and Thread class doesn't have a getLooper() method; but Android's main thread does. Can Main Thread be accessed as a HandlerThread
?
If you take a look at the source code, you will see that the thread inside the looper is not of type
HandlerThread
:No