JobScheduler
Before getting started with what, how and when with JobScheduler lets first understand its importance and why is it preferred over AlarmManager and SyncAdapter.
Lets take a scenario where there is a need of accessing network and fetching the updated data in a loop from server say after an hour or two. In this case lets see what exactly among the three i.e AlarmManager, SyncAdapter and JobScheduler fits the best and why.
Case 1 : Disadvanatages of using AlarmManager
AlarmManager is blank when it comes about the device or battery i.e AlarmManager has got no idea on whats going on with the device such as- The device current situation.
- Whether the battery is getting charged.
- Running low on battery.
- Is the user going to incur additional charges on the network if they download the data.
- Is the device going to reboot.
Case 2 : Disadvanatages of using SyncAdapter
- Tricky to use.
- Lot of logic required.
- Its buggy.
- Lot of errors.
- Lack of documentation.
- Not the best for periodic tasks.
Case 3 : JobScheduler proves to advantageous
The disadvantages mentioned above under AlarmManager and SyncAdapter is handled now by JobScheduler. JobScheduler is called as intelligent background processing. It has got all the idea and can perform action- when the data connection is fast/cheap.
- when the device is charging.
- when the device is running low on battery.
- When need to wake up and perform periodic tasks.
Advantages of using JobScheduler API
- Can batch more than one job when resources are available.
- Jobs scheduled with this API is non-user facing jobs.
- Similar jobs can be combined to optimize the battery usage.
- It manages automatically when the network is unreliable and also survives when the application is restarted thus makes it easier in handling the uploads.
When to use JobScheduler
- It can be used when there is a need to perform any task periodically.
- It can be used when there is no user interaction and a task is to be run in the background.
- It is to be used when you want to ensure you run a task in background when there is network connection or WiFi access.
- It can be used when there is a task that is to be run once the device is connected to power supply.
Types of Constraints with JobScheduler API
Simple Constraints
- When a job is to be run only when the device is charging.
- When a job is to be run only when the device battery is low.
- when device is idle.
- when a job is to be run after certain interval.
- Run a job before specific time.
Complex Constraints - Combining all the constraints to run a job scheduler at.
- Run a job at every specific interval, when the device is charging and also when the wifi is connected.
How to use JobScheduler
Step 1 : Define the job constraints using JibInfo.Builder.Step 2 : Use the build method to create JobInfo Object that represents all your constraints.
Step 3 : Define a class that extends JobService.
- Inside onStart() - define the work you want to do that by default runs on the main thread.
- Inside onJobFinished() - you can reschedule the job or consider the task is done.
- Inside onStop() - indicates the job execution is completed.
Step 4 : From JobScheduler class, schedule() method is called and pass job input object containing the constraints to JobInfo Object.Each time JobScheduler.schedule() is called JobService class is triggered and the tasks is performed within it.
Summary : JobScheduler schedules as many jobs together to run asynchronously leading to minimum usage of battery.
Please do share your thoughts and any queries on this post in the comments below and please do like my post by giving +1 and by sharing it.
0 Comments