Sitemap
Amit Dhiman

Amit Dhiman

Sr. Software Developer, Frontend developer with 9+ years of experience. Specializing in Kotlin, Java, Flutter, Angular, Javascript, Typescript, AWS

Handler and Looper

2 min readJul 2, 2023

--

Complete detail about handler and looper

Handler:

  • A Handler is an Android class that allows you to schedule and execute code on a specific thread, typically the main/UI thread.
  • It provides a way to post messages (or Runnables) to the message queue of a thread and process them sequentially.
  • Handlers are useful for performing tasks on a specific thread without blocking or freezing the UI.

Looper:

  • A Looper is responsible for managing the message queue of a thread. Each thread has its own Looper associated with it.
  • The Looper continuously loops through the message queue, retrieves messages, and dispatches them for processing.
  • By default, only the main/UI thread has a Looper prepared, allowing it to receive and process messages.
  • Other threads don’t have a Looper prepared by default, and you need to explicitly create and manage one if you want to use Handlers on those threads.

Key Concepts:

Message Queue:

  • A message queue is a data structure that holds messages or Runnables for processing by a Looper.
  • Messages are added to the message queue using Handlers or by directly posting them to the Looper.
  • The Looper takes messages one by one from the queue and dispatches them for processing.

Runnable:

  • A Runnable is a piece of code that can be executed.
  • It can be posted to a Handler’s message queue for execution on the associated thread.
  • Runnables are typically used for executing tasks or updating the UI.

HandlerThread:

    Get Amit Dhiman’s stories in your inbox

    Join Medium for free to get updates from this writer.

  • HandlerThread is a convenient class in Android that combines a thread and a Looper together.
  • It simplifies the creation and management of a background thread with a Looper and a message queue.

Usage:

Posting Messages/Runnables:

  • Use handler.post() or handler.postDelayed() to post a message or Runnable to the associated thread’s message queue for execution.

Handling Messages/Runnables:

  • Override the handleMessage() method of a Handler to define the logic for processing messages.
  • This method is automatically invoked when a message is retrieved from the message queue.

Background Tasks:

  • Create a Handler on a background thread (e.g., using a HandlerThread) to perform tasks in the background.
  • Post messages or Runnables to the Handler to execute code on the background thread.
  • Use the main/UI thread’s Handler to update the UI with the results obtained from the background thread.

Updating UI from a Background Thread:

  • Use the main/UI thread’s Handler to post Runnables that update UI elements.
  • By posting Runnables to the main thread’s Handler, the UI updates are executed on the main/UI thread, ensuring thread safety.

Communication between Threads:

  • Handlers can be used for inter-thread communication by sending messages or Runnables between threads.
  • Multiple threads can communicate with each other by exchanging messages using Handlers.

Looper.loop():

  • The Looper.loop() method is called on the thread’s Looper to start processing messages from the message queue.
  • It is a blocking operation that runs indefinitely until the Looper’s quit() method is called, signaling the termination of the loop.

Handlers and Loopers provide a powerful mechanism for managing and scheduling tasks on different threads in an Android application. They facilitate communication, coordination, and synchronization between threads, ensuring that tasks are executed in a controlled manner while adhering to the specific requirements of each thread.

--

--

Amit Dhiman
Amit Dhiman

Written by Amit Dhiman

Sr. Software Developer, Frontend developer with 9+ years of experience. Specializing in Kotlin, Java, Flutter, Angular, Javascript, Typescript, AWS