Search code examples
androidjob-schedulingandroid-workmanager

PeriodicWorkRequest trigger time


I wanna add a trigger time to start work on but I can't find a way to do that with work manger
that's My code

 PeriodicWorkRequest alarmWork =
            new PeriodicWorkRequest.Builder(
                    AlarmWorker.class,
                    interval ,
                    TimeUnit.MILLISECONDS
            ).setInputData(bundle).build();
    WorkManager.getInstance().enqueue(alarmWork)

Solution

  • You won't be able to specify exact time at which a Work should be executed. If you need a task to be executed at exact time regardless of Doze mode, you should use AlarmManager.

    However you can setup initial delay with WorkManager as follows:

    OneTimeWorkRequest mywork=
            new OneTimeWorkRequest.Builder(MyWorker.class)
            .setInitialDelay(12, TimeUnit.HOURS)
            .build();
    WorkManager.getInstance().enqueue(mywork);