The first step in using the JobScheduler API is to create a job service class. It extends the SDK JobService superclass and includes two required methods named onStartJob() and onStopJob(). The onStartJob() method is called by the system when the job is executed. The parameters that are passed in include the job ID you assign when you schedule the job, any extra values you pass in, and a bunch of other useful information.
- [Instructor] The first step in using the JobScheduler API…is to create a job service.…Android Studio 2.3 doesn't have an automatic tool…to create this kind of service,…but you can create a normal service…and then change it as needed.…So, I'll go to my App Module…and I'll select New, Service, Service,…and I'll call it MyJobService.…The job service does not need to be exported,…that is it doesn't have to be made available…to the rest of the device,…so I'll uncheck that option and click Finish.…
This kind of service extends the class JobService.…Now, it doesn't need this onBind method.…That's for bound services.…And I can also get rid of all these other imports.…When I move the cursor over the arrow indicator,…I see two issues.…Let's deal with the API level first.…The JobScheduler is only available starting in API level 21.…You can either add conditional logic…around all the calls to the job service…or you can simply set your minimum SDK to at least API 21…and that's what I'll do in my Gradle build file.…
I'll change my minimum SDK version to 21 and then resync.…
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