Загрузка страницы

How to Start a Background Thread in Android

When we try to execute long running operations on the app's main thread (also called the UI thread), we freeze the app's user interface and after a couple seconds we get an "application not responding" dialog message with the option to force quit the app. To avoid this we have to move heavy operations like database manipulation and network requests onto a separate thread.
In this video we will learn how to create such a separate worker thread in Android. For this we can either create a class that extends Thread and override it's run method, or create a class that implements the Runnable interface, and then pass this Runnable to a new Thread object.
These core Java classes build the foundation for classes like AsyncTask, HandlerThread and ThreadPoolExecutor.
We will also learn how to send code from the background thread back to the UI thread by calling post on a Handler that sends a runnable to the MessageQueue of the UI thread. To associate the Handler with the Looper of the main thread, we either instantiate it on the main thread, or pass Looper.getMainLooper to the constructor.
Instead of creating a handler, we can also use the View classes post method or the Activity classes runOnUiThread method, that use the main handler internally.

Great explainer video by the Android developer team about threads, handlers, loopers etc:
https://www.youtube.com/watch?v=0Z5MZ0jL2BM

Watch this playlist to learn more about Looper, MessageQueue & Handler:
https://www.youtube.com/playlist?list=PLrnPJCHvNZuDl4WUKi26WbzLMsM1sXCD4

Example code:
https://codinginflow.com/tutorials/android/starting-a-background-thread

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

❗ Subscribe to the channel:
https://www.youtube.com/c/codinginflow?sub_confirmation=1

💲 Earn money by becoming an affiliate for my courses:
https://codinginflow.com/affiliate

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

💻 Hire me for your next project:
https://codinginflow.com/contact

💚 Donate to Coding in Flow with a message:
https://paypal.me/codinginflow

❓ Ask your programming questions in our chat:
https://discord.gg/TSnMvmc

📣 Follow Coding in Flow on other sites:
Facebook: https://www.facebook.com/codinginflow
Instagram: https://www.instagram.com/codinginflow
Twitter: https://twitter.com/codinginflow
Github: https://github.com/codinginflow

Видео How to Start a Background Thread in Android канала Coding in Flow
Показать
Комментарии отсутствуют
Введите заголовок:

Введите адрес ссылки:

Введите адрес видео с YouTube:

Зарегистрируйтесь или войдите с
Информация о видео
1 мая 2018 г. 15:49:51
00:16:02
Яндекс.Метрика