androidgoogle-cloud-messagingtaskscheduled-tasksgcmtaskservice

How do schedule a GcmTaskService to run every day at a specific time


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);

Solution

  • 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.