The thread (a HandlerThread
) is started in onCreate
, when you call thread.start();
, then you get a reference to the Looper
of that thread (only one Looper
is created per HandlerThread
) to create a Handler
and the Handler
is used to post messages to the thread. The Looper
is the object that waits for the messages in a while(true)
loop.
Every time a command is sent to the Service
, the Service
posts a message to the HandlerThread
through the Handler
.
A closer look at the source code will help you understand better how it all works. There's an excelente post about Handler
s and Looper
s at Square Engineering Blog - A journey on the Android Main Thread - Part 1.
You can also use an IntentService to avoid instantiating your own threads.