The Handler class lets you manage a thread's message queue. Each thread has a Looper object that receives and dispatches messages. For example, if the user clicks a button, that creates a message that the Looper receives and then dispatches to the appropriate part of the app framework that handles such things. A Handler lets you add messages to the queue, either right away or at particular times.
- [Instructor] Java code runs sequentially by default.…That means that a code block runs from start to end…within a single thread…until the sequence of actions is complete.…The UI thread,…also referred to sometimes as the main thread,…can only do one thing at a time,…and if you try to do more than one thing,…you'll block the UI, causing it to become nonresponsive.…I'll demonstrate this by going to this bit of code,…a method named runCode.…Right now it's just displaying a bit of text,…and I'll show the result in my emulator.…
When I click the Run Code button,…I get some text down at the bottom…and the scroll view that's wrapping the text view…scrolls down to the bottom.…Now, after I've called the log method,…I'll add this text.…Thread.sleep, and I'll pass in a value of 3,000.…That means pause the code for three seconds.…The sleep method can throw an exception…so I'll use an intention action…and wrap that with a try catch block.…Now this code looks like I should see the new text…and then there should be a three second pause,…
First, discover how to create and start simple background threads, and how to use handlers to manage a thread's message queue. Then, learn various methods for optimizing the scheduling and performance of background tasks in Android with AsyncTask, intent services, and the JobScheduler API. Plus, explore tools that help you implement multithreading for different tasks in Android: Loader, for asynchronous data loading, and the open-source API Retrofit, for making HTTP requests.
Note: To get the most out of this course, you should be comfortable programming with Java, and should understand the most basic skills that are needed to build Android apps with the Android SDK and Android Studio.
- Creating and running a background thread
- Sending messages to the UI from threads
- Managing multiple background threads
- Managing threads with AsyncTask
- Managing long-running tasks with services
- Scheduling background tasks with JobScheduler
- Using other APIs for concurrent programming