Force GcmTaskService to start

I am trying to start GcmTaskService

without waiting 30 seconds . If the task is not successful, it should follow the usual GcmTask

s rules .

but to prevent abuse, the scheduler will set a minimum alarm of 30 seconds in the future. Your task may still be started earlier than this if some network event occurs to wake up the scheduler.

This does not work:

public static void start(Context context) {
    context.startService(new Intent(context, FileDownloadService.class).setAction(
            SERVICE_ACTION_EXECUTE_TASK));
}

      

I would rather not have 2 services that do the same thing.

+3


source to share


1 answer


You cannot start the service in GcmTaskService b / c since Google Play Services starts your task. If you absolutely must, you can override onStartCommand () (make sure you call super.onStartCommand when you're done checking if the intent is what you expect). If you did it, it would look like

public int onStartCommand(...) {
    if ("my.app.TRIGGER_TASK".equals(intent.getAction())) {
        // run your task logic.
    }
    return super.onStartCommand(...);
}

      

However, if you run your task in onStartCommand, it will run your task on the main thread of the application so you can't block I / O.



Also, you are likely to run into problems where the GcmTaskService takes care of calling the stopService () function when it detects that a previously running task has completed.

Alternatively (and more securely) you can bind your GcmTaskService long enough to start your task. You can safely execute onRunTask () logic on the binder thread. You will have to handle your own sync.

+2


source







All Articles