0

Basically it's a periodic GcmNetworkManager task that should run every day at a specific time, it's persistent so it survives across reboots, it's period is 86400000L (24h), but i need it to run the first time on a specific time.

I've already created the TaskService class, added it to the manifest and created the task, is there a way of doing this?

This is my TaskService class:

public class AgendaTaskService extends GcmTaskService {

private static final String TAG = "AgendaTaskService";
public static final String TAG_TASK_UPDATE_EVENTS = "AgendaTaskService";

@Override
public int onRunTask(TaskParams taskParams) {

        Log.i(TAG,"Running task");

        switch (taskParams.getTag()){

            case TAG_TASK_UPDATE_EVENTS:
                Log.i(TAG,"Updating agenda events");

                //TODO HERE IS WHERE MY TASK WILL RUN

                return GcmNetworkManager.RESULT_SUCCESS;

            default: return GcmNetworkManager.RESULT_FAILURE;

        }

    }

}

And this is how I'm creating the task:

 GcmNetworkManager gcmNetworkManager = GcmNetworkManager.getInstance(this);
    PeriodicTask task = new PeriodicTask.Builder()
            .setService(AgendaTaskService.class)
            .setTag(AgendaTaskService.TAG_TASK_UPDATE_EVENTS)
            .setPersisted(true)
            .setPeriod(30L)
            .setUpdateCurrent(true)
            .build();
    gcmNetworkManager.schedule(task);
Rafael
  • 1,437
  • 6
  • 23
  • 41

2 Answers2

2

You can schedule a OneOffTask to trigger your PeriodicTask

user3829751
  • 712
  • 7
  • 20
  • 1
    This should be the accepted answer. If AlarmManager were a desirable approach then would likely be using it in the first place and not GcmNetworkManager. – click_whir Mar 31 '17 at 22:36
  • Thanks, but I suppose it's worth noting that `AlarmManager` may be still be preferable if strict timings are desired. Device idle / Doze mode will defer `GcmNetworkManager` tasks and `JobScheduler` jobs – user3829751 Apr 07 '17 at 15:46
0

Use AlarmManager to schedule a PendingIntent to start a Service at your trigger time, and do whatever GCM-specific scheduling you need when that service runs.

Jeffrey Blattman
  • 22,176
  • 9
  • 79
  • 134