简体   繁体   中英

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

您可以安排一个OneOffTask来触发您的PeriodicTask

使用AlarmManager安排PendingIntent在触发时启动Service ,并在该服务运行时执行所需的GCM特定日程安排。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM