| java.lang.Object | |
| ↳ | com.google.android.gms.gcm.GcmNetworkManager |
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;
}
}
| 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 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
Cancels all tasks previously scheduled by this application.
| |||||||||||
Cancel a task, specified by tag.
| |||||||||||
Use this function to access the Network Scheduler API.
| |||||||||||
Schedule a task that will recur until the user calls one of
cancelAllTasks(), or
cancelTask(String) with the tag provided here. | |||||||||||
Schedule a task to occur at some point within the specified window.
| |||||||||||
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
| |||||||||||
Action broadcast by the scheduler to the requesting package when a scheduled task is ready for execution.
Indicates a task has failed, but not to reschedule.
Indicates a task has failed to execute, and must be retried with back-off.
Indicates a task has successfully been executed, and can be removed from the queue.
Cancels all tasks previously scheduled by this application.
Cancel a task, specified by tag.
| tag | The tag passed in to schedulePeriodicTask(long, long, String) or
scheduleTask(long, long, String). Can be null, which indicates the
default operation.
|
|---|
Use this function to access the Network Scheduler API.
| context | Context of the calling app. |
|---|
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).
| 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. |
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.
| 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. |
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.
| endPoint | The class that implements your task's logic. |
|---|