Job Scheduler in Android
In this blog, We are going to learn about Job Scheduler. JobScheduler is introduced while the Android set limitation on background Execution. It means that the system will automatically destroy the background execution to save battery and memory.
So if you want to perform some background job like some network operations no matter your app is running or not. Here comes the Job scheduler. You can take this as a modern Alarm manager. The job scheduler is a more intelligent alarm manager. If you have some operation that is based on time then you have to use the alarm manager in that scenario. But Job Scheduler has its own criteria like If the device connected with wifi or network connection or it is currently charging.
Usually, It will batch different jobs together to be more efficient. The job executes on the basis of that criteria.
We can create a JobScheduler by extending a class from JobService. It is a substitute of Intent service while the intent service gets deprecated. JobService used main thread by default we have to manage the threads while doing long-running jobs like network operations.
Here is the example of Job Scheduler :
1 2 3 4 5 6 7 8 9 10 11 |
class MyJobScheduler : JobService(){ override fun onStartJob(p0: JobParameters?): Boolean { return false } override fun onStopJob(p0: JobParameters?): Boolean { return false } } |
We will have to override two methods.
onStartJob :
onStartJob will be called when the job execution gets started. It takes JobParameters as a parameter. You can the job info like job id by using that parameter. The return type for this job is Boolean that means you have to inform the system whether you want to start the job again after finishing their job or not. If you want to cancel the job after finishing the execution then simply return false which means the system will call onStopJob method and automatically cancel the specified job.
onStopJob :
onStop Job will be called by the system. When the service is running and some of the criteria are not satisfying the service then the android system will automatically call onStop Job. Let suppose you have registered a job scheduler that required a network connection and your service is running. Then you disable your net connection. Then the android system will call the onStopJob. It is also returning a Boolean value that means you want to register the job again or you want to let it finish.
Here we have to do some additional changes to our Job Scheduler.
<service android:name=”.MyJobScheduler” android:permission=”android.permission.BIND_JOB_SERVICE” />
If you want your service to be prevented from booting or Restarting the android device so you have to add boot complete permission and set your job setPersisted(true) in your JobInfo.
1 |
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" |
Now the last part comes into the picture execution of the service.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
class MainActivity : AppCompatActivity() { val JOB_ID = 1001; val REFRESH_INTERVAL : Long = 15 * 60 * 1000 // 15 minutes override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) scheduleJob() } private fun scheduleJob() { val jobScheduler = applicationContext .getSystemService(JOB_SCHEDULER_SERVICE) as JobScheduler val componentName = ComponentName(this, MyJobScheduler::class.java) val jobInfo = JobInfo.Builder(JOB_ID, componentName) .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY) .setPeriodic(REFRESH_INTERVAL) .build() jobScheduler.schedule(jobInfo) } } |
First of all, we have to take system services for “JOB_SCHEDULER_SERVICE”. Then we have to create our component name object that takes two parameters first is context and the second is your Service class.
The last and main part is JobInfo you have to build your JobInfo object by using JobInfo.Builder class. It will contain all your information regarding the job like the job id all the criteria that are required for your service.
I suggest you read all the criteria that JobInfo provides to be a master in scheduling the jobs.
Here we have done. Hope this blog will help you to understand the Job Scheduler in Android.
Reference link –> https://developer.android.com/reference/android/app/job/JobScheduler,
https://developer.android.com/reference/android/app/job/JobInfo,
https://developer.android.com/reference/android/app/job/JobService