0

I am developing an application where certain tasks as to be done in background at a specific time.

Android developer docs suggests various methods. I know AlarmManager can be used for this purpose. But, I think, using AlarmManager in Android 6.0 in Doze mode will give improper triggers.

I came across JobScheduling libraries from developer docs

As my application supports version below Android 5.0. I thought of using FireBaseJobDispatcher.

Can I use FirebaseJobDispatcher for trigger a specific task at a specific time.

For Example : I want to update the DB once every hour from 9AM to 6PM

John
  • 8,846
  • 8
  • 50
  • 85

1 Answers1

1

You can, but if you time window is not so strict, there is no guarantee that your task start to work in a few hours later, because Doze mode. If you want really strict scheduling - use AlarmManager.setAndAllowWhileIdle. And, as I know FirebaseJobDispatcher has not released yet, and there is a few unresolved issues. Before it releases, you can use GCMNetworkManager (from Google Play Services com.google.android.gms:play-services-gcm) for this purpose. Your code for FirebaseJobDispatcher may looks like this:

final FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(context));
if (isSyncEnabled) {

    Job job = dispatcher.newJobBuilder()
            .setService(SyncService.class)
            .setTag(TAG)
            .setConstraints(isSyncOnlyWiFi ? Constraint.ON_UNMETERED_NETWORK : Constraint.ON_ANY_NETWORK)
            .setRecurring(true) // !!!
            .setLifetime(Lifetime.FOREVER) //!!!
            .setTrigger(Trigger.executionWindow(syncIntervalInSeconds, syncIntervalInSeconds + TIME_WINDOW_IN_SECONDS))
            .setReplaceCurrent(true)
            .setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL)
            .build();

    final int result = dispatcher.schedule(job);
    if (result != FirebaseJobDispatcher.SCHEDULE_RESULT_SUCCESS) {
        // pre handle error
    }

For GcmNetworkManager:

PeriodicTask task = new PeriodicTask.Builder()
            .setService(SyncManagerService.class)
            .setTag(TASK_TAG_SYNC_SERVICE)
            .setUpdateCurrent(true)
            .setPersisted(true)
            .setPeriod(period)
            .setFlex(flexInSeconds < period ? flexInSeconds : period)
            .setRequiredNetwork(wifiOnly ? PeriodicTask.NETWORK_STATE_UNMETERED : PeriodicTask.NETWORK_STATE_CONNECTED)
            .build();

GcmNetworkManager.getInstance(context).schedule(task);
Roman_D
  • 4,680
  • 2
  • 14
  • 17
  • i think FJD is released 0.5.0 https://github.com/firebase/firebase-jobdispatcher-android/releases – John Oct 07 '16 at 05:30
  • Yes, it looks like FJD released, thanks. but still, not stable, as for me. – Roman_D Oct 07 '16 at 05:34
  • What about GCM network manager . Is it stable ? And Can it be used below lollipop ? – John Oct 07 '16 at 05:39
  • 1
    i got it . GCM Network Manager is a compatibility port of the Android JobScheduler for apps that support versions of Android prior to 5.0 (API level 21) – John Oct 07 '16 at 05:45