I am trying to create an Android app that increments a counter after every 24 hours. I am just making use of SharedPreferences and when it is time it increments the value in shared preferences. Right now I made use of FirebaseJobDispatcher but the problem with this was that it is inconsistent in incrementing. I read through some documents and my understanding is that it is better to use FirebaseJobDispatcher when it involves some network calls as well. My question is that how can I schedule a simple job in android to run every 24 hours? Please any help and suggestions would be very helpful. Below is my FirebaseJobDispatcher code. Right now I am just using using 2 minutes just for checking purposes.
public class ScheduleIncrementJob {
private static final int UPDATE_INTERVAL_MINUTES = 2;
private static final int UPDATE_INTERVAL_SECONDS = (int)(TimeUnit.MINUTES.toSeconds(UPDATE_INTERVAL_MINUTES));
private static final int SYNC_FLEX_SECONDS = UPDATE_INTERVAL_SECONDS;
private static final String UPDATE_JOB_TAG = "update_counter_tag";
private static boolean sInitialized;
synchronized public static void scheduleUpdateCounter(final Context context){
if(sInitialized)return;
Driver driver = new GooglePlayDriver(context);
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(driver);
Job constraintUpdateJob = dispatcher.newJobBuilder()
.setService(IncrementJobService.class)
.setTag(UPDATE_JOB_TAG)
.setLifetime(Lifetime.FOREVER)
.setRecurring(true)
.setTrigger(Trigger.executionWindow(UPDATE_INTERVAL_SECONDS,
UPDATE_INTERVAL_SECONDS + SYNC_FLEX_SECONDS))
.setReplaceCurrent(true)
.build();
dispatcher.schedule(constraintUpdateJob);
sInitialized = true;
}
}