Android APIs
public class

GcmNetworkManager

extends Object
java.lang.Object
   ↳ com.google.android.gms.gcm.GcmNetworkManager

Class Overview

Clients will use this class to schedule tasks that require network access.

This uses a simple pattern of requests and callbacks that make it easy to set up clearly defined network task execution schedules.

Tasks must be scheduled based on an execution window in time. During this execution window the scheduler will use its discretion in picking an optimal execution time, based on network availability (whether the device has connectivity), network activity (whether packages are actively being transferred). and load (how many other pending tasks are available for execution at that point in time).

To receive the notification from the scheduler that a task is ready to be executed, the client app must implement a GcmTaskService and filter on the action ACTION_TASK_EVENT. The receivers must be registered in the manifest in order to wake up the application.

Similar to the pattern used in GoogleCloudMessaging, the service should be protected by the permission com.google.android.gms.permission.BIND_NETWORK_TASK_SERVICE held by Google Play services. This prevents other code from invoking the broadcast receiver. Here is an excerpt from a sample manifest:

 <service android:name=".MyUploadService" 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>

A task event will have an associated state, and optionally any user-specified data given to the scheduler in the schedule request.


     // Schedule a task to occur between five and fifteen seconds from now:
     GcmNetworkManager.get(this)
                 .setService(MyUploadService.getClass())
                 .scheduleTask(5L, 15L, "upload");
     ...

     // Implement service logic to be notified when the task elapses:
     MyUploadService extends GcmTaskService {
         @Override
         public int onRunTask(String tag) {
             // Do some upload work.
             return GcmNetworkManager.RESULT_SUCCESS;
         }
     }
 

Summary

Constants
String ACTION_TASK_EVENT Action broadcast by the scheduler to the requesting package when a scheduled task is ready for execution.
int RESULT_FAILURE Indicates a task has failed, but not to reschedule.
int RESULT_RESCHEDULE Indicates a task has failed to execute, and must be retried with back-off.
int RESULT_SUCCESS Indicates a task has successfully been executed, and can be removed from the queue.
Public Methods
void cancelAllTasks()
Cancels all tasks previously scheduled by this application.
void cancelTask(String tag)
Cancel a task, specified by tag.
synchronized static GcmNetworkManager get(Context context)
Use this function to access the Network Scheduler API.
void schedulePeriodicTask(long periodInSecs, long flexInSecs, String tag)
Schedule a task that will recur until the user calls one of cancelAllTasks(), or cancelTask(String) with the tag provided here.
void scheduleTask(long windowStartSecs, long windowEndSecs, String tag)
Schedule a task to occur at some point within the specified window.
GcmNetworkManager setService(Class<? extends GcmTaskService> endPoint)
If your package has more than one GcmTaskService you must differentiate between them at schedule-time by using this setter.
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final String ACTION_TASK_EVENT

Action broadcast by the scheduler to the requesting package when a scheduled task is ready for execution.

Constant Value: "com.google.android.gms.gcm.ACTION_TASK_READY"

public static final int RESULT_FAILURE

Indicates a task has failed, but not to reschedule.

Constant Value: 2 (0x00000002)

public static final int RESULT_RESCHEDULE

Indicates a task has failed to execute, and must be retried with back-off.

Constant Value: 1 (0x00000001)

public static final int RESULT_SUCCESS

Indicates a task has successfully been executed, and can be removed from the queue.

Constant Value: 0 (0x00000000)

Public Methods

public void cancelAllTasks ()

Cancels all tasks previously scheduled by this application.

public void cancelTask (String tag)

Cancel a task, specified by tag.

Parameters
tag The tag passed in to schedulePeriodicTask(long, long, String) or scheduleTask(long, long, String). Can be null, which indicates the default operation.

public static synchronized GcmNetworkManager get (Context context)

Use this function to access the Network Scheduler API.

Parameters
context Context of the calling app.
Returns
  • GcmNetworkManager object.

public void schedulePeriodicTask (long periodInSecs, long flexInSecs, String tag)

Schedule a task that will recur until the user calls one of cancelAllTasks(), or cancelTask(String) with the tag provided here. The task is persisted so that if the phone powers down it will continue to be executed. Note that periodic tasks will not be scheduled if their period is below a certain minimum (currently 30 seconds).

Parameters
periodInSecs Number of seconds between subsequent executions of this task.
flexInSecs Number of seconds before the period elapses that this task may run.
tag Tag to identify this operation.

public void scheduleTask (long windowStartSecs, long windowEndSecs, String tag)

Schedule a task to occur at some point within the specified window. The task is persisted so that if the phone powers down it will be executed when restarting. If one of cancelTask(String) or cancelAllTasks() is called before this executes it will be cancelled. Note that you can request a one-off task to be executed at any point in the future, 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.

Parameters
windowStartSecs Seconds from now, beginning at which the task may be executed.
windowEndSecs Seconds from now, at which the task must have been executed.
tag Tag to identify this operation.

public GcmNetworkManager setService (Class<? extends GcmTaskService> endPoint)

If your package has more than one GcmTaskService you must differentiate between them at schedule-time by using this setter. Before you hand your task to the scheduler service it will be queried against the package manager and if there is more than one service that can handle ACTION_TASK_EVENT an IllegalArgumentException will be thrown and your application will crash.

Parameters
endPoint The class that implements your task's logic.
Returns
  • This object, for convenience.