Looper, MessageQueue & Handler Explained - Android Studio Tutorial
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:
https://www.youtube.com/playlist?list=PLrnPJCHvNZuDl4WUKi26WbzLMsM1sXCD4
Example code:
https://gist.github.com/codinginflow/40b4935c89a9fd27cce914ad738526d3
____________________
? Find the BEST programming tutorials on TutHub:
https://tuthub.io
⭐ Get my MVVM Caching Course now:
https://codinginflow.com/caching
❗ Subscribe to the channel:
https://www.youtube.com/c/codinginflo...
? Subscribe to the Coding in Flow newsletter:
https://codinginflow.com/newsletter
❓ Join our free developer community:
https://discord.gg/TSnMvmc
? Follow Coding in Flow on other sites:
Facebook: https://www.facebook.com/codinginflow
Instagram: https://www.instagram.com/codinginflow
TikTok: https://www.tiktok.com/@codinginflow
Twitter: https://twitter.com/codinginflow
Github: https://github.com/codinginflow
? Business requests, sponsoring, etc.: [email protected]
Что делает видео по-настоящему запоминающимся? Наверное, та самая атмосфера, которая заставляет забыть о времени. Когда вы заходите на RUVIDEO, чтобы посмотреть онлайн «Looper, MessageQueue & Handler Explained - Android Studio Tutorial», вы рассчитываете на нечто большее, чем просто загрузку плеера. И мы это понимаем. Контент такого уровня заслуживает того, чтобы его смотрели в HD 1080, без дрожания картинки и бесконечного буферизации.
Честно говоря, Rutube сегодня — это кладезь уникальных находок, которые часто теряются в общем шуме. Мы же вытаскиваем на поверхность самое интересное. Будь то динамичный экшн, глубокий разбор темы от любимого автора или просто уютное видео для настроения — всё это доступно здесь бесплатно и без лишних формальностей. Никаких «заполните анкету, чтобы продолжить». Только вы, ваш экран и качественный поток.
Если вас зацепило это видео, не забудьте взглянуть на похожие материалы в блоке справа. Мы откалибровали наши алгоритмы так, чтобы они подбирали контент не просто «по тегам», а по настроению и смыслу. Ведь в конечном итоге, онлайн-кинотеатр — это не склад файлов, а место, где каждый вечер можно найти свою историю. Приятного вам отдыха на RUVIDEO!
Видео взято из открытых источников Rutube. Если вы правообладатель, обратитесь к первоисточнику.