The Handler class is used to post messages to a thread’s message queue. But there are many actions that you might want to run in a completely separate thread that runs simultaneously with the app’s main thread. The lowest level way of doing this is to create your own Java thread objects. Like the Handler class, a thread needs a Runnable object—an implementation of the Runnable interface—that defines the work to be done.
- [Instructor] The handler class which I demonstrated previously is used to post messages to a thread's message queue, but you're still on the main thread. There are many actions that you might want to run at a completely separate thread that runs simultaneously with the app's main thread and the lowest level way of doing this is to create your own Java thread object. Like the handler class, a thread needs a runnable object, an implementation of the runnable interface that defines the work to be done. In this code which I created with the handler class, I'm going to change the run method.
First of all, I'll get rid of the call to display progress bar. When you're running in a background thread, you can't do anything that directly touches the user interface, so I can't call code for example that makes a view object visible or invisible. Next, I'll change my first call to logcat to say starting thread for four seconds. Then, as I did previously, I'll call thread.sleep and I'll pass in a value of 4,000 for 4,000 milliseconds.
As always when I call sleep, I have to wrap that in a try-catch block. Then after the sleep is complete, I'll once again use logcat output and I'll output the message ending thread. Now to use this version of the runnable object, I won't use a handler. Instead, I'll use the Java thread class and this is a member of java.lang. I'll instantiate it with its constructor, with new thread, and I'll pass the runnable object to it.
Notice how many different versions of the constructor there are. Then to create and run the new thread, I'll call thread.start. There's also a method named run, but you don't want to use that. It doesn't actually create a new thread. If you call run instead of start, your runnable object's code will run but it will still be on the UI thread and you'll still be blocking the user interface. Use start and not run. Also notice again that I'm only logging output using the logcat tool.
I'm not trying to touch the user interface because I can't do that within a thread. So next, I'll run the application on my emulator and I'll bring the emulator into the foreground so I can see it overlaid on top of Android Studio. I'll make sure I can see my logcat output and then I'm filtering on the code runner tag. I'll click the button and I see the message in the background that I'm running the thread and I can still scroll up and down while the thread is running so I know that the background processing is working, but notice that my progress bar is still running.
Right now, I don't have any way for the thread to notify the user interface that its work is complete. I'll show you how to do that in the next video, but that's the core of how to use the thread class. Using the thread class, you can create and start your own custom threads. The code won't have access to the UI thread, but you'll be able to execute background operations such as retrieving data from the network, saving the results in a database, or doing other work that the user doesn't have to see.
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