Preparing the Looper

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() { 
         handler = LooperHandler() 

In this example, we demonstrated basic steps to program a Looper class. Do not forget to prepare() your Looper class or you will get an exception and your application can crash!

Get Mastering Android Development with Kotlin now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.