Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • How to Schedule Tasks in Android Using JobScheduler API

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 2.20k
    Comment on it

    Job Scheduling Api

    If your app used long repetitive task, you must be using activities and mostly the services and timer tasks but sometimes system stops the services and activities to free up memory and space.



    Android system currently uses Alarm Manager and JobSchedular to achieve the repetitive task, where JobSchedular is the latest one and pretty useful.From Android 5.0(Lollipop)  added the Job scheduling API.


    JobScheduler API has a process of batch scheduling of jobs. Battery consumption can be reduced by combining the jobs.Uploads are easier using JobManager. Job scheduler also handles when the application restarts.

    Read the following when should we can use this API.

    • network access or a Wi-Fi connection tasks.

    • Not critical tasks



    Jobinfo object specifies the scheduling functionality The job scheduler allows to consider the state of the device, You can use the JobInfo.Builder class to allow and configure schedule task to run.

    Note the following points for scheduling task under some conditions:

    • Phone  charging

    • Phone is connected network

    • Phone is idle

    • Starts bynext hour

    For implementing Job, you should user JobService class and implement the onStartJob and onStopJob methods. onStartJob is performed on the UI thread.

    JobService should be registered in the Android manifest with the BIND_JOB_SERVICE permission.


                android:label="Demo service"
                android:permission="android.permission.BIND_JOB_SERVICE" >



    Create Job

     * JobService to be scheduled by the JobScheduler.
     * start another service
    public class DemoJobService extends JobService {
        private static final String TAG = "DemoService.class.getName";
      //The onStartJob is performed in the main thread, if you start asynchronous processing in this 
      //method, return true otherwise false.
        public boolean onStartJob(JobParameters params) {
            Intent service = new Intent(getApplicationContext(), LocalService.class);
            Util.scheduleJob(getApplicationContext()); // reschedule the job
            return true;
       //If the job fails for some reason, return true from on the onStopJob to restart the job. 
        public boolean onStopJob(JobParameters params) {
            return true;


    Add Receiver Class

    Register  receiver  android.intent.action.BOOT_COMPLETED broadcast and Schedule the Job

    /*Broadcast receiver
    public class TestServiceReceiver extends BroadcastReceiver {
        public void onReceive(Context context, Intent intent) {
           //event fire when boot is completed  


    //Register the receiver in the Android manifest

    <receiver android:name="TestServiceReceiver" >
        <action android:name="android.intent.action.BOOT_COMPLETED" />


    public class Utility {
        // scheduling  start of the Jobservice every 10 - 20 secs
        public static void scheduleJob(Context context) {
            ComponentName serviceComponent = new ComponentName(context, DemoJobService.class);
            //create and schedule a jobinfo builder
            JobInfo.Builder builder = new JobInfo.Builder(0, serviceComponent);
            builder.setMinimumLatency(1 * 1000); // waiting time
            builder.setOverrideDeadline(20 * 1000); // max delay
            // device idle true
             // works even on charging and discharging
             JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);


    How to Schedule Tasks in Android Using JobScheduler API

 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: