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.…
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