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