Nuked
Nuked

Reputation: 391

PeriodicWorkRequest don't respect the repetition time

i have a problem with the work request (1.0.0-alpha10) Let say if i want to launch a work (in my case a notification) every 5 hours i do the following

static void StartWorker(){
PeriodicWorkRequest workRequest = new PeriodicWorkRequest.Builder(NotifyWorker.class, 5, TimeUnit.HOURS,3000,TimeUnit.MILLISECONDS)
            .addTag("ok")
           .build();
}

But the problem is that the notification start even after 20 minutes, is weird because is like is really random, i understand that can't pass exactly 5 hours between two notifications but anticipate it of 4:30 hours is a little too much.

The NotifyWorker class is like this:

public class NotifyWorker extends Worker {

public NotifyWorker(@NonNull Context context, @NonNull WorkerParameters params) {
    super(context, params);
}

@NonNull
@Override
public Worker.Result doWork() {

    // Method to trigger an instant notification

    Context context= getApplicationContext();

    notification();

    return Worker.Result.SUCCESS;

}

}

What i'm doing wrong? I'm using a Galaxy S8 with Android 8.0.0 for debugging.

Upvotes: 2

Views: 1020

Answers (1)

Basha K
Basha K

Reputation: 1519

According to my understanding Flex Interval is the minimum waiting time after which the task can be executed. So you have to change the the flex interval to 295 minutes (4hours 55 minutes) from 300 milli seconds .Example from google documentation periodic work request

}

Upvotes: 2

Related Questions