2

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.

Nuked
  • 341
  • 3
  • 12
  • 2
    Please don't downvote without a valid reason. If you have to, explain the reason why you're downvoting. – Phillip Oct 21 '18 at 15:50

1 Answers1

2

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

}

Basha K
  • 1,509
  • 1
  • 11
  • 16
  • 2
    Correct! Just a note: if you are debugging an app like this, is better to uninstall completely it before start a new one because i notice that is like android keep the old notification stored in memory – Nuked Oct 23 '18 at 19:22