Let's explain the Looper
class. We used it in previous examples but we did not explain it in details.
Looper
represents a class that is used to execute messages
or runnable
instances in a queue. Ordinary threads do not have any queue like the Looper
class has.
Where we can use the Looper
class? For the execution of multiple messages
or runnable
instances, Looper
is needed! One example of use can be adding new tasks to the queue while, at the same time, the operation of task processing is running.
To use the Looper
class, we must first call the prepare()
method. When Looper
is prepared, we can use the loop()
method. This method is used to create a message
loop in the current thread. We will give you one short example:
class LooperHandler : Handler() { override fun handleMessage(message: Message) { ... } } class LooperThread : Thread() { var handler: Handler? = null override fun run() {...