Hi, in this video, we're going to talk about how to start a background task in your Android application. Now, in Java, a runnable task is a unit of work. And so we can assign things like the priority level of the job, and we can also set it to be delayed so that it might be run in the future. So we give it a timestamp. So this is what it looks like when you create a runnable task in Java. You say, runnable, give it a name, and then make it a new runnable class. It has one method inside of it called run. And so the purpose of a runnable task is so that you can break it up into different threads. Now, a thread is a process or a pathway that a process goes through. And so the main thread is usually what we program on, because we think of a sequential program. So let's take a look at some code of how this might be done. So what I have in front of here is a sample of a click listener for a button. And so we have an onclick of it. Now, what we have inside is a little different. I created a new thread, and inside of that thread, I created what's called a unit of work. So we'll look at that in just a second. That's really a runnable process. And then we tell the thread to start. So what this will do is it'll say, don't touch the main thread where we're listening for mouse clicks. Let's start another thread. This probably has a lower priority, and we'll let the process run there. So you might want to say, let's download a file or let's query a database or let's draw something or render an image. And so the process is likely going to take a while. Most of the time, we don't have to worry about how long something takes because processes are nearly instantaneous. But in this case, pretending that we have something very slow. Let's look at the next code. So this is what the unit of work looks like. This is the runnable process. So the runnable process is going to have a single method called run. And then in my case, we're going to code here, is that I'm going to simulate something that takes five seconds. And so I just have the delay statement here, or the sleep statement that says, whatever you're doing, stop, and don't go any further for 5,000 milliseconds. Now, when we get to this end of it, I want to pop up something on the screen. I want it to put a toast. And to do a toast, you have to be able to run that on the main thread. And so we have to create another runnable process inside of this runnable. So this runnable has got a command that's special, it's unique to Android, it says run on UI thread. So that means go to the main thread that was handling all the button clicks, and then do this process. And you can see that a toast is something that is the user interface. If this were something else, like print to a log file or just compute an answer and return it, then you wouldn't have to worry about running on the user interface thread. But if you have to update a text field or change the color of a button or, in this case, do a toast, you're touching the UI thread. And so that's why we tell it which thread to run on. And so this unit of work would be what we would put in the button click. Now, there's another way to handle this. Instead of using run on UI thread, most programmers would use this. They would call a handler. So remember, the handler is like the robot arm that picks up the task and puts it on the conveyor belt. So the handler says, I'm going to create a new instance of the handler object, and I'm going to specify that this is from the main Looper. And then inside here, you can tell me that I'm going to post a new runnable object on the main loop. And so posting a new item. And so that's like inserting it into the conveyor belt in the factory floor. And when you POST, you can assume that it's going to be handled immediately. But there are other commands, such as POST with a delay. And so you can set up a timer to say, do this in five seconds, and then it'll wait before that is actually executed. So those are some examples of how you make runnable tasks and how you POST them and how you protect your main loop, your user interface, so that it doesn't get locked up. So we'll code some of this, and we'll see an example of how it works.