How to call getLooper() on main Thread?

1.1k views Asked by At

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 ?

1

There are 1 answers

0
Eng.Fouad On

If you take a look at the source code, you will see that the thread inside the looper is not of type HandlerThread:

60       final Thread mThread;
...
188      mThread = Thread.currentThread();

Can Main Thread be accessed as a HandlerThread

No