Android-job: How to make a task run on the same day and every consequent day after that at the specified time

Created on 28 Feb 2017  路  4Comments  路  Source: evernote/android-job

Hi! First of all, thank you for the excellent library! Can I just ask a question? How to make the job run once it has been specified and after that at every consequent time. For example, if the current time in the system is 9:40 am. And I schedule a task to run at 10:00am today and every consequent day. How should my job request builder class look like?

This is the current state that does not run at 10:00 am on the same day it as been scheduled.

 `public static int schedule(Date time, boolean updateCurrent) {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, time.getHours());
    calendar.set(Calendar.MINUTE, time.getMinutes());

    long startMs = calendar.getTimeInMillis();

    long endMs = startMs + TimeUnit.MINUTES.toMillis(5);

    int id = new JobRequest.Builder(TAG)
            .setExecutionWindow(startMs, endMs)
            .setPersisted(true)
            .setUpdateCurrent(updateCurrent)
            .build()
            .schedule();

    return id;
}`

Note: the execution window is small on purpose (start time + 5 minutes). It has to be ~10:00 each day.

question

Most helpful comment

You're scheduling the job 17225 days in the future. I doubt that you want to wait that long :) Notice that the start and end time is an offset from the current time. E.g. your start time should be

long startMs = System.currentTimemillis() - calendar.getTimeInMillis();

All 4 comments

Hi, have you seen the FAQ? https://github.com/evernote/android-job/blob/master/FAQ.md#how-can-i-run-a-job-at-a-specific-time-once-a-day

I think this snippet should help you.

Thank you for your answer! Yes, I saw the FAQ before and actually this is how I found how to schedule a job that runs every day at a specific time. However, the code above does not run on the same day once schedules. For example, If I schedule a task at 9:40am for 10:00am (the code above) it dose not run at all. I see this in the console when the taks is schedules but never runs at 10:00am. Not sure what I am doing wrong:
D/JobProxy24: Schedule one-off jobInfo success, request{id=1, tag=StartSleepingHoursJob}, start 10:00:31 (+17225 days), end 10:05:31 (+17225 days), reschedule count 0

You're scheduling the job 17225 days in the future. I doubt that you want to wait that long :) Notice that the start and end time is an offset from the current time. E.g. your start time should be

long startMs = System.currentTimemillis() - calendar.getTimeInMillis();

Thank you for your suggestion! Really did not notice that.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ruhul015 picture ruhul015  路  4Comments

Haoxiqiang picture Haoxiqiang  路  3Comments

tatocaster picture tatocaster  路  6Comments

karntrehan picture karntrehan  路  5Comments

vibin picture vibin  路  3Comments