Looper, MessageQueue & Handler Explained - Android Studio Tutorial

Veröffentlicht am: 25 März 2019
auf dem Kanal: Coding in Flow
93,355
2k

In this tutorial, I will explain what Android's Looper, MessageQueue, and Handler classes are, how they are used to keep the UI thread alive, and how we can add them to a normal Java thread. This is the concept behind the HandlerThread class, but in this video we will set up everything manually to understand the underlying concepts.
To create a looper thread, we have to call Looper.prepare in a Thread's run method to attach a Looper and a MessageQueue to it. When we call Looper.loop, the Looper loops through this MessageQueue in an infinite for-loop until we call quit on it. In each round, the Looper takes the next message with the current timestamp from the MessageQueue and dispatches it to the Handler that originally added it to the queue, where it will then be executed on the thread this Handler is attached to.
A Handler can send a message with arbitrary data fields (what, arg1, arg2, obj etc.) or post a runnable, which will then be added as a callback to an empty message internally. Optionally, a delay or time can be specified, for example with postDelayed or sendMessageDelayed. Messages without a callback (data messages) have to be handled by overriding the handleMessage method in a Handler subclass or callback.
We will also take a quick look at ways to avoid memory leaks from non-static inner classes.

Watch the whole playlist:
   • Playlist  

Example code:
https://gist.github.com/codinginflow/...

____________________
💻 Find the BEST programming tutorials on TutHub:
https://tuthub.io

⭐ Get my MVVM Caching Course now:
https://codinginflow.com/caching

❗ Subscribe to the channel:
   / codinginflo.  .

📨 Subscribe to the Coding in Flow newsletter:
https://codinginflow.com/newsletter

❓ Join our free developer community:
  / discord  

📣 Follow Coding in Flow on other sites:
Facebook:   / codinginflow  
Instagram:   / codinginflow  
TikTok:   / codinginflow  
Twitter:   / codinginflow  
Github: https://github.com/codinginflow

💰 Business requests, sponsoring, etc.: info@codinginflow.com


Auf dieser Seite können Sie das Online-Video Looper, MessageQueue & Handler Explained - Android Studio Tutorial mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer Coding in Flow 25 März 2019 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 93,355 Mal angesehen und es wurde von 2 tausend den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!