When a task’s work is done, you have an opportunity to return a result. Just as with reporting progress, you can’t touch the UI from the background thread—but you can return a value and then handle it in a separate method running on the main thread.
- [Instructor] When a task's work is complete, you have an opportunity to return a result. Just as with reporting progress, you can't touch the UI from the background thread. But you can return a value, and then handle it in a separate method running on the main thread. To return the value from the background thread, use the standard Java return statement. So I'll go through my AsyncTask definition to the doInBackground method. And where I currently have this statement return null, I'll say return "thread all done." And I can return that value because it's a string.
And I declared the type of a string as the second parameter in my generic declaration. Now, to handle that return value, override the method onPostExecute. The type that will be generated in Android Studio will match the type that you declare in the AsyncTask itself. Just as with onProgressUpdate, the superclasses version of the method is just a stub. It won't do anything, it might even throw an exception. So I'll delete that call, and then I'll call the log method.
Now, notice onPostExecute only receives one value. onProgressUpdate and doInBackground receive variable length arrays. But you can only return a single value. If you wanted to set the type of the return value with something more complex, such as a list or a map, you could use that type to encapsulate more values. But declaring a string will result in returning just one string. I'll call my log method, and pass in the value that I receive from the argument named s, and I'll run that code.
I see the values output one at a time and one per second. And then for each task, I see the message "thread all done." Remember, I'm creating two instances of the task in my runCode method up here. So each of those tasks executes, then finishes on its own. Just like with onProgressUpdate, the onPostExecute method runs on the main thread, so it can make changes to view objects or do anything else in the user interface.
The AsyncTask framework lets you easily manage these background threads and communicate with the main thread without the scattered code that can result from using the thread and handler classes.
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