I'm trying to start a GcmTaskService without waiting the 30 seconds. If the task is not successful, then it should follow the usual GcmTasks' rules.
but to prevent abuse the scheduler will only set an alarm at a minimum of 30 seconds in the future. Your task can still be run earlier than this if some network event occurs to wake up the scheduler.
This doesn't work:
public static void start(Context context) {
context.startService(new Intent(context, FileDownloadService.class).setAction(
SERVICE_ACTION_EXECUTE_TASK));
}
I'd rather not have 2 services that do the same exact thing.
You can't startService on a GcmTaskService b/c that is how Google Play Services starts your task. If you absolutely needed to you could override onStartCommand() (make sure to call super.onStartCommand when you are done checking whether the intent matches what you expect). If you did this it would look something like
public int onStartCommand(...) {
if ("my.app.TRIGGER_TASK".equals(intent.getAction())) {
// run your task logic.
}
return super.onStartCommand(...);
}
That said, if you run your task in onStartCommand it is going to be running your task on your app's main thread so you can't do any blocking i/o.
Also you will probably run into issues where the GcmTaskService takes care of calling stopService() on itself when it detects that a previously executing task is finished.
Alternatively (and safer) you could bind to your GcmTaskService just long enough to start up your task. You can safely do the onRunTask() logic on the binder thread. You'd have to handle your own synchronization.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With