You can schedule a job from anywhere in your app. It can be upon app startup if the job executes tasks that are critical to running the app, or upon a user event, such as a button click or visiting a particular activity. Create a JobInfo object that describes the constraints to apply—how soon the job should be executed, required conditions such as network availability, and whether a job should be repeated multiple times.
- [Instructor] Once you've created your job service class and you've registered the service in the app manifest and provided the required permission, you're ready to schedule the job. I'll do that in my main activity class in my runCode method. I'll start by creating an instance of the JobScheduler class. You create this using a call to getSystemService and you pass in the following constant, Context.Job_Scheduler_Service.
You'll need to cache the return value as the correct type, JobScheduler. Next, create an instance of the JobInfo class. You can create this object using the JobInfo.Builder class. I'll start with new JobInfo.Builder and I have to pass in a job ID, an integer. This can be any number you like. I'll pass in a value of 1001, but if you're going to reference the job again from this context, you should turn this into a constant.
I'll name the constant JOB_ID. Next, pass in an instance of the ComponentName class. This takes two arguments. The first argument is the current context, I'll pass in this, and the next is the service class that you want to use to construct the job. Pass in the class' class property. Next, you need to pass in at least one constraint. A constraint is a rule for the JobScheduler that indicates when the job can be executed.
Now, this doesn't guarantee exactly when the job will happen. It simply sets minimum requirements. The simplest available requirement or constraint is called with setMinimumLatency. This is the minimum amount of time that you want to pass before the job is executed. And if you pass in a value of zero, that means execute the job as soon as you possibly can. Then finally, to return the jobInfo object, call the build method. So, now I have a scheduler and a jobInfo object and I can schedule the job.
I'll call JobScheduler.schedule and pass in the jobInfo object and I'm ready to start the job. I'll run the app in my emulator and make sure that I can see my monitor window so I can see the logcat output. When I click the run code button, that schedules the job and on my system, the job was executed pretty much right away. That may not happen on your system depending on what else is happening on the device. Again, you're just scheduling the job, requesting that it be executed, but it's up to the scheduler to decide exactly when it happens.
Each time I click run code, the job is constructed and run again. If I run it a few times in quick succession, that'll schedule the job but you might see a delay in the additional executions. There are many other constraints that you can apply and you can learn about them by looking at the methods that are available in the builder. For example the setPeriodic method means that you want to run the job multiple times at this interval. If I call setPeriodic and pass in a value of 5000, that means I want to run the job every five seconds approximately.
setRequiredNetworkType means I can't run this job until I know I have the right kind of network available. You can say any kind of network. You can say you only want to run the job on a non-metered network only when you're not roaming and there are other options as well. And you'll see plenty of other constraints available. For example, you can also say I only want to run this job if the battery isn't low. Take a look at the documentation for jobInfo.Builder for more information on all of the constraints you can apply when you schedule a job through the JobScheduler.
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