0

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;
}
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
RCR
  • 43
  • 7

1 Answers1

0

You can use AlarmManager class to schedule a repeating alarm with PendingIntent that will do your job when scheduled. https://developer.android.com/training/scheduling/alarms

I'll give you sample of dailyAlarm doing some job when fired:

 public void setDailyAlarmOn(Context context, long alarmTime, Uri reminderTask, long repeatTime) {
        AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);


        PendingIntent operation =
                yourJobHere.getReminderPendingIntent(context, reminderTask);

        if (Build.VERSION.SDK_INT >= 23) {

            manager.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime, repeatTime, operation);
        } else {
            manager.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime,  repeatTime, operation);
        }

    }

alarmTime - first time in miliseconds when alarm is supposed to be fired
repeatTime - time in mili seconds - 86400000 for 24h (1000*60*60*24)
Uri reminderTask - uri that I created to not to cancel previous alarms, in short, it's a code for alarm application and it's uri from row in database.
operation - PendingIntent which you'll need to create

I did it by extending IntentService and handling job in onHandleIntent: YourJobHere.class:

public class YourJobHere extends IntentService {
    private static final String TAG = YourJobHere.class.getSimpleName();

    public static PendingIntent getReminderPendingIntent(Context context, Uri uri) {
        Intent action = new Intent(context, YourJobHere.class);
        action.setData(uri);
        return PendingIntent.getService(context, 0, action, PendingIntent.FLAG_UPDATE_CURRENT);
    }

    public YourJobHere() {
        super(TAG);
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {

        //TODO: make your job here

      }
}

If you want you can refer to my full project on GitLab: https://gitlab.com/Domin_PL/SlothProfileScheduler

Hope it'll help you

Domin
  • 1,075
  • 1
  • 11
  • 28