The code that runs in the doInBackground() method of an AsyncTask can’t communicate with the activity’s main thread. But just like with threads and handlers, there’s a way to pass information to another method that is running on the main thread. When you declare a subclass of AsyncTask, its second generic type is for progress values. The type can be a simple string, but you can also pass complex objects with multiple properties.
- [Instructor] The code that runs in the doInBackground method of an AsyncTask can't communicate with the activity's main thread. But just like with threads and handlers, there's a way to pass information to another method that is running on the main thread. And the AsyncTask architecture makes this even easier. When you declare a subclass of AsyncTask you declare three generic parameters, types, and I've set these as strings. The first type is the type of the parameters that are being passed in to doInBackground.
The second is the type of progress updates. And the third is the type of the result that's returned at the end of the task. To send information to the main thread from within doInBackground, call a method named publishProgress. Now, to demonstrate this I'll change my for loop. And each time I log the message doInBackground to Logcat, I'll also call publishProgress. The publishProgress method takes a variable array of values, and the types of those values have to match the second generic type that you declare in the class.
I'm just going to pass back a single value, and it would be the value that I get from the strings that were passed in as parameters. Now, if when I call publishProgress nothing will happen on its own. To receive that value and act on it, override a method called onProgressUpdate. I'll add that here after doInBackground, and I'll start typing the name of the method, onProgressUpdate, and then I'll select it. Once again, I'm receiving a variable array of values. The super classes version of the method doesn't do anything, it's just a stub, so I'll delete that call.
And then, instead, I'll call my custom log method and I'll pass in the first value. With the array notation values . Now, I know that I only passed in one value here, and therefore, I can safely receive one value here. But if I know that I passed in more than one value I would change the way I'm processing this update method. And that's all there is to it. The runInBackground method runs on the background thread, and onProgressUpdate runs on the main thread.
Here's the result, I'll clear my screen, and then click run code, and I see the three values displayed. Red, green, blue, and then the values from the second task, pink, orange, and purple. Now, if I go and look at my Android monitor, I'll see that the values are being duplicated in Logcat. And that's because I still have Logcat messages in both the background method and in the foreground method that handles the progress update. You get the same result as with explicit thread and handler objects, but all of the code is in one place, your subclass of the AsyncTask class.
You no longer have to explicitly place messages in bundles, add those to a message object, and then dispatch the message, then create a complex receiver architecture on the other side. You simply publish the progress, and then you listen for and react on the main thread. And you can pass any Java object you like between the threads. You just need to set the type of the progress objects when you declare your custom task.
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