GCM Network Manager - Periodic Task not firing

◇◆丶佛笑我妖孽 提交于 2019-12-05 08:22:56

The problem is that you are overriding onStartCommand! This is how Google Play Services executes the task on your GcmTaskService. If you want it to work simply

return super.onStartCommand(intent, flags, startId);

Perhaps it's worth mentioning that the reason onRunTask is provided is so you don't have to worry about your Service's lifecycle - you can rely on the internals of GcmTaskService to stopService when required.

That said, if you startService() on your GcmTaskService with custom intents you will likely mess this up and end up with a Service that isn't stopped when it should be.

If you do need to call in to the GcmTaskService (not recommended) you should bind to it - that interface is completely untouched by the GcmTaskService internals.

@Override
public int onRunTask(TaskParams taskParams)
{
    Log.info("onRunTask: " + taskParams.getTag());

    return GcmNetworkManager.RESULT_SUCCESS;
}

@Override
public void onCreate()
{
    Log.i(TAG, "in onCreate");
    super.onCreate();
    GcmNetworkManager.getInstance(this).schedule(createPeriodicSyncTask());
}

private PeriodicTask createPeriodicSyncTask()
{
    return new PeriodicTask.Builder()
            .setService(PeriodicSyncService.class)
            .setPeriod(mSyncIntervalInSec)
            .setFlex(mSyncIntervalInSec - 20)
            .setTag(TAG)
            .setPersisted(true)
            .setRequiredNetwork(NETWORK_STATE_ANY)
            .setUpdateCurrent(true)
            .setRequiresCharging(false)
            .build();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!