I am trying to use GcmNetworkManager to schedule a recurring task in my application that ends up to API level 17. I installed everything as described on the GCM Network Manager page ( https://developers.google.com/cloud-messaging/network-manager ):
In my AndroidManifest.xml, I have:
<service android:name=".services.MyService" android:exported="true" android:permission="com.google.android.gms.permission.BIND_NETWORK_TASK_SERVICE"> <intent-filter> <action android:name="com.google.android.gms.gcm.ACTION_TASK_READY"/> </intent-filter> </service>
In my application, I have:
long periodSecs = 30L; // the task should be executed every 30 seconds long flexSecs = 15L; // the task can run as early as -15 seconds from the scheduled time String tag = "myScan|1"; PeriodicTask periodic = new PeriodicTask.Builder() .setService(MyService.class) .setPeriod(periodSecs) .setFlex(flexSecs) .setTag(tag) .setPersisted(false) .setRequiredNetwork(com.google.android.gms.gcm.Task.NETWORK_STATE_ANY) .setRequiresCharging(false) .setUpdateCurrent(true) .build(); GcmNetworkManager.getInstance(this).schedule(periodic);
And I have a MyService that looks like this:
public class MyService extends GcmTaskService { @Override public int onRunTask(TaskParams taskParams) { Log.info("onRunTask: " + taskParams.getTag()); return GcmNetworkManager.RESULT_SUCCESS; } @Override public int onStartCommand (Intent intent, int flags, int startId) { Log.info("onStartCommand"); return GcmTaskService.START_STICKY_COMPATIBILITY; } }
When I launch the application, I can get onStartCommand as expected, but onRunTask will never be called. Am I missing something? I expect that as soon as it starts (as indicated by the launch of the start command), it should start every 15-30 seconds - is this the correct assumption? Why doesn't he shoot at all?
Thanks!
source share