How do I call getLooper () on the main thread?

On Android, Main Thread and HandlerThread

Looper and MessageQueue are set by default. I can call getLooper () on the handlerThread object, but why not include the main theme?

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-world scenario, you should never use getLooper () on mainThread; we can just call Looper.getMainLooper()

. I just would like to know why this is not working.

I understand this in terms of Java, what it Looper.getMainLooper().getThread()

returns java.lang.Thread

, and the Thread class doesn't have a getLooper () method; but the main Android thread. How can I access the main topic HandlerThread

?

+3


source to share


1 answer


If you look at the source code, you can see that the stream inside the looper has no type HandlerThread

:

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

      




The main topic can be accessed as HandlerThread

Not

+2


source







All Articles