John John - 1 year ago 578
Android Question

Can i use Firebase JobDispatcher for triggering time specific tasks

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

Answer Source

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 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()
            .setConstraints(isSyncOnlyWiFi ? Constraint.ON_UNMETERED_NETWORK : Constraint.ON_ANY_NETWORK)
            .setRecurring(true) // !!!
            .setLifetime(Lifetime.FOREVER) //!!!
            .setTrigger(Trigger.executionWindow(syncIntervalInSeconds, syncIntervalInSeconds + TIME_WINDOW_IN_SECONDS))

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

For GcmNetworkManager:

PeriodicTask task = new PeriodicTask.Builder()
            .setFlex(flexInSeconds < period ? flexInSeconds : period)
            .setRequiredNetwork(wifiOnly ? PeriodicTask.NETWORK_STATE_UNMETERED : PeriodicTask.NETWORK_STATE_CONNECTED)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download