GCM Network Manager - periodic task not to shoot

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!

+6
source share
2 answers

The problem is that you are overriding onStartCommand! So Google Play Services performs this task on your GcmTaskService. If you want it to work just

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

It may be worth mentioning that the reason for onRunTask is provided, so you don’t have to worry about the life cycle of your service - you can rely on the internals of GcmTaskService to stop the service when it is needed.

However, if you run Service () on your GcmTaskService with custom intentions, you will probably mess it up and get a service that won't be stopped when it is.

If you need to call the GcmTaskService (not recommended), you must bind to it - this interface is not completely affected by the internal components of the GcmTaskService.

+9
source
 @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(); } 
+1
source

All Articles