JobScheduler class is used to schedule Jobs in Android.
JobScheduler was added recently in API 21, so below that it will not work.
Here is a simple example…
Create JobScheduler Service
We need to create the JobSchedulerService class at first.
package jobscheduler.coderzheaven.com.demo1; import android.app.job.JobParameters; import android.app.job.JobService; import android.util.Log; public class JobSchedulerService extends JobService { private static final String TAG = "JobSchedulerService"; @Override public boolean onStartJob(JobParameters params) { Log.i(TAG, "onStartJob:"); return false; } @Override public boolean onStopJob(JobParameters params) { Log.i(TAG, "onStopJob:"); return false; } }
AndroidManifest
Just like a service, we need to add this service in the AndroidManifest.
<service android:name=".JobSchedulerService" android:permission="android.permission.BIND_JOB_SERVICE" android:exported="true"/>
Create Scheduler Objects
Now we will create the JobScheduler Object and schedule the Job that run every 3 seconds.
package jobscheduler.coderzheaven.com.demo1; import android.app.job.JobInfo; import android.app.job.JobScheduler; import android.content.ComponentName; import android.content.Context; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; JobScheduler mJobScheduler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mJobScheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE); JobInfo.Builder builder = new JobInfo.Builder(1, new ComponentName(getPackageName(), JobSchedulerService.class.getName())); builder.setPeriodic(3000); builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY); if (mJobScheduler.schedule(builder.build()) <= 0) { Log.e(TAG, "onCreate: Some error while scheduling the job"); } } }
More Parameters
- setMinimumLatency(long minLatencyMillis): This makes your job not launch until the stated number of milliseconds have passed. This is incompatible with setPeriodic(long time) and will cause an exception to be thrown if they are both used.
- setOverrideDeadline(long maxExecutionDelayMillis): This will set a deadline for your job. Even if other requirements are not met, your task will start approximately when the stated time has passed. Like setMinimumLatency(long time), this function is mutually exclusive with setPeriodic(long time) and will cause an exception to be thrown if they are both used.
- setPersisted(boolean isPersisted): This function tells the system whether your task should continue to exist after the device has been rebooted.
- setRequiredNetworkType(int networkType): This function will tell your job that it can only start if the device is on a specific kind of network. The default is JobInfo.NETWORK_TYPE_NONE, meaning that the task can run whether there is network connectivity or not. The other two available types are JobInfo.NETWORK_TYPE_ANY, which requires some type of network connection available for the job to run, and JobInfo.NETWORK_TYPE_UNMETERED, which requires that the device be on a non-cellular network.
- setRequiresCharging(boolean requiresCharging): Using this function will tell your application that the job should not start until the device has started charging.
- setRequiresDeviceIdle(boolean requiresDeviceIdle): This tells your job to not start unless the user is not using their device and they have not used it for some time.
All Done. Now run the app and check the logs.
This is a bit out of date – for newer Android versions, the period must be at least 15 minutes and will automatically be raised if it’s lower. Also, this doesn’t seem to work if the application is for some reason removed from memory.