Implemented by the client application to provide an endpoint for the GcmNetworkManager
to call back to when a task is ready to be executed.
Clients must add this service to their manifest and implement
onRunTask(com.google.android.gms.gcm.TaskParams)
.
This service must provide an IntentFilter
on the action
SERVICE_ACTION_EXECUTE_TASK
. Here's an example:
<service android:name="MyTaskService" android:permission="com.google.android.gms.permission.BIND_NETWORK_TASK_SERVICE" android:exported="true"> <intent-filter> <action android:name="com.google.android.gms.gcm.ACTION_TASK_READY"/> </intent-filter> </service>
The return value of onRunTask(TaskParams) will determine what the manager does with subsequent
executions of this task. Specifically you can return RESULT_RESCHEDULE
to have this task be re-executed again shortly subject to exponential back-off. Returning
RESULT_FAILURE
for a periodic task will only affect the executing
instance of the task, and future tasks will be executed as normal.
Once a task is running it will not be cancelled, however a newly scheduled task with the same
tag will not be executed until the active task has completed. This newly scheduled task will
replace the previous task, regardless of whether the previous task returned
RESULT_RESCHEDULE
.
Bear in mind that your service may receive multiple calls from the scheduler at once
(specifically if you've made multiple schedule requests that overlap). If this is the case, your
implementation of onRunTask(com.google.android.gms.gcm.TaskParams)
must be thread-safe.
The scheduler will hold a PowerManager.WakeLock
for your service, however
after three minutes of execution if your task has not returned it will be considered to
have timed out, and the wakelock will be released. Rescheduling your task at this point
will have no effect.
If you suspect your task will run longer than this you should start your own service
explicitly or use some other mechanism; this API is intended for relatively quick network
operations.
Your task will run at priority Process.THREAD_PRIORITY_BACKGROUND. If this is not appropriate, you should start your own service with suitably conditioned threads.
Constant Summary
String | SERVICE_ACTION_EXECUTE_TASK | Action broadcast by the GcmNetworkManager to the requesting package when a scheduled task is ready for execution. |
String | SERVICE_ACTION_INITIALIZE | Action that a GcmTaskService is started with when the service needs to initialize
its tasks. |
String | SERVICE_PERMISSION | You must protect your service with this permission to avoid being bound to by an application other than Google Play Services. |
Inherited Constant Summary
int | START_CONTINUATION_MASK | |
int | START_FLAG_REDELIVERY | |
int | START_FLAG_RETRY | |
int | START_NOT_STICKY | |
int | START_REDELIVER_INTENT | |
int | START_STICKY | |
int | START_STICKY_COMPATIBILITY |
String | ACCESSIBILITY_SERVICE | |
String | ACCOUNT_SERVICE | |
String | ACTIVITY_SERVICE | |
String | ALARM_SERVICE | |
String | APPWIDGET_SERVICE | |
String | APP_OPS_SERVICE | |
String | AUDIO_SERVICE | |
String | BACKUP_SERVICE | |
String | BATTERY_SERVICE | |
int | BIND_ABOVE_CLIENT | |
int | BIND_ADJUST_WITH_ACTIVITY | |
int | BIND_ALLOW_OOM_MANAGEMENT | |
int | BIND_AUTO_CREATE | |
int | BIND_DEBUG_UNBIND | |
int | BIND_IMPORTANT | |
int | BIND_NOT_FOREGROUND | |
int | BIND_WAIVE_PRIORITY | |
String | BLUETOOTH_SERVICE | |
String | CAMERA_SERVICE | |
String | CAPTIONING_SERVICE | |
String | CARRIER_CONFIG_SERVICE | |
String | CLIPBOARD_SERVICE | |
String | CONNECTIVITY_SERVICE | |
String | CONSUMER_IR_SERVICE | |
int | CONTEXT_IGNORE_SECURITY | |
int | CONTEXT_INCLUDE_CODE | |
int | CONTEXT_RESTRICTED | |
String | DEVICE_POLICY_SERVICE | |
String | DISPLAY_SERVICE | |
String | DOWNLOAD_SERVICE | |
String | DROPBOX_SERVICE | |
String | FINGERPRINT_SERVICE | |
String | HDMI_CONTROL_SERVICE | |
String | INPUT_METHOD_SERVICE | |
String | INPUT_SERVICE | |
String | JOB_SCHEDULER_SERVICE | |
String | KEYGUARD_SERVICE | |
String | LAUNCHER_APPS_SERVICE | |
String | LAYOUT_INFLATER_SERVICE | |
String | LOCATION_SERVICE | |
String | MEDIA_PROJECTION_SERVICE | |
String | MEDIA_ROUTER_SERVICE | |
String | MEDIA_SESSION_SERVICE | |
String | MIDI_SERVICE | |
int | MODE_APPEND | |
int | MODE_ENABLE_WRITE_AHEAD_LOGGING | |
int | MODE_MULTI_PROCESS | |
int | MODE_PRIVATE | |
int | MODE_WORLD_READABLE | |
int | MODE_WORLD_WRITEABLE | |
String | NETWORK_SCORE_SERVICE | |
String | NETWORK_STATS_SERVICE | |
String | NFC_SERVICE | |
String | NOTIFICATION_SERVICE | |
String | NSD_SERVICE | |
String | PERSISTENT_DATA_BLOCK_SERVICE | |
String | POWER_SERVICE | |
String | PRINT_SERVICE | |
String | RESTRICTIONS_SERVICE | |
String | SEARCH_SERVICE | |
String | SENSOR_SERVICE | |
String | STORAGE_SERVICE | |
String | TELECOM_SERVICE | |
String | TELEPHONY_SERVICE | |
String | TELEPHONY_SUBSCRIPTION_SERVICE | |
String | TEXT_SERVICES_MANAGER_SERVICE | |
String | TV_INPUT_SERVICE | |
String | UI_MODE_SERVICE | |
String | USAGE_STATS_SERVICE | |
String | USB_SERVICE | |
String | USER_SERVICE | |
String | VIBRATOR_SERVICE | |
String | WALLPAPER_SERVICE | |
String | WIFI_P2P_SERVICE | |
String | WIFI_RTT_SERVICE | |
String | WIFI_SCANNING_SERVICE | |
String | WIFI_SERVICE | |
String | WINDOW_SERVICE |
int | TRIM_MEMORY_BACKGROUND | |
int | TRIM_MEMORY_COMPLETE | |
int | TRIM_MEMORY_MODERATE | |
int | TRIM_MEMORY_RUNNING_CRITICAL | |
int | TRIM_MEMORY_RUNNING_LOW | |
int | TRIM_MEMORY_RUNNING_MODERATE | |
int | TRIM_MEMORY_UI_HIDDEN |
Public Constructor Summary
Public Method Summary
IBinder | |
void |
onInitializeTasks()
When your package is removed or updated, all of its network tasks are cleared by the
GcmNetworkManager.
|
abstract int | |
int |
onStartCommand(Intent intent, int flags, int startId)
Receives the command to begin doing work, for which it spawns another thread.
|
Inherited Method Summary
void | |
final Application |
getApplication()
|
abstract IBinder |
onBind(Intent arg0)
|
void |
onConfigurationChanged(Configuration arg0)
|
void |
onCreate()
|
void |
onDestroy()
|
void |
onLowMemory()
|
void |
onRebind(Intent arg0)
|
void |
onStart(Intent arg0, int arg1)
|
int |
onStartCommand(Intent arg0, int arg1, int arg2)
|
void |
onTaskRemoved(Intent arg0)
|
void |
onTrimMemory(int arg0)
|
boolean |
onUnbind(Intent arg0)
|
final void |
startForeground(int arg0, Notification arg1)
|
final void |
stopForeground(boolean arg0)
|
final void |
stopSelf()
|
final void |
stopSelf(int arg0)
|
final boolean |
stopSelfResult(int arg0)
|
void |
attachBaseContext(Context arg0)
|
boolean | |
int |
checkCallingOrSelfPermission(String arg0)
|
int |
checkCallingOrSelfUriPermission(Uri arg0, int arg1)
|
int |
checkCallingPermission(String arg0)
|
int |
checkCallingUriPermission(Uri arg0, int arg1)
|
int |
checkPermission(String arg0, int arg1, int arg2)
|
int |
checkSelfPermission(String arg0)
|
int | |
int |
checkUriPermission(Uri arg0, int arg1, int arg2, int arg3)
|
void |
clearWallpaper()
|
Context |
createConfigurationContext(Configuration arg0)
|
Context |
createDisplayContext(Display arg0)
|
Context |
createPackageContext(String arg0, int arg1)
|
String[] |
databaseList()
|
boolean |
deleteDatabase(String arg0)
|
boolean |
deleteFile(String arg0)
|
void | |
void | |
void | |
void | |
void | |
void | |
void | |
String[] |
fileList()
|
Context |
getApplicationContext()
|
ApplicationInfo |
getApplicationInfo()
|
AssetManager |
getAssets()
|
Context |
getBaseContext()
|
File |
getCacheDir()
|
ClassLoader |
getClassLoader()
|
File |
getCodeCacheDir()
|
ContentResolver |
getContentResolver()
|
File |
getDatabasePath(String arg0)
|
File |
getDir(String arg0, int arg1)
|
File |
getExternalCacheDir()
|
File[] |
getExternalCacheDirs()
|
File |
getExternalFilesDir(String arg0)
|
File[] |
getExternalFilesDirs(String arg0)
|
File[] |
getExternalMediaDirs()
|
File |
getFileStreamPath(String arg0)
|
File |
getFilesDir()
|
Looper |
getMainLooper()
|
File |
getNoBackupFilesDir()
|
File |
getObbDir()
|
File[] |
getObbDirs()
|
String |
getPackageCodePath()
|
PackageManager |
getPackageManager()
|
String |
getPackageName()
|
String |
getPackageResourcePath()
|
Resources |
getResources()
|
SharedPreferences |
getSharedPreferences(String arg0, int arg1)
|
Object |
getSystemService(String arg0)
|
String |
getSystemServiceName(Class<?> arg0)
|
Resources.Theme |
getTheme()
|
Drawable |
getWallpaper()
|
int |
getWallpaperDesiredMinimumHeight()
|
int |
getWallpaperDesiredMinimumWidth()
|
void | |
boolean |
isRestricted()
|
FileInputStream |
openFileInput(String arg0)
|
FileOutputStream |
openFileOutput(String arg0, int arg1)
|
SQLiteDatabase |
openOrCreateDatabase(String arg0, int arg1, SQLiteDatabase.CursorFactory arg2, DatabaseErrorHandler arg3)
|
SQLiteDatabase | |
Drawable |
peekWallpaper()
|
Intent | |
Intent | |
void |
removeStickyBroadcast(Intent arg0)
|
void | |
void |
revokeUriPermission(Uri arg0, int arg1)
|
void | |
void | |
void |
sendBroadcast(Intent arg0)
|
void | |
void | |
void | |
void | |
void | |
void |
sendOrderedBroadcastAsUser(Intent arg0, UserHandle arg1, String arg2, BroadcastReceiver arg3, Handler arg4, int arg5, String arg6, Bundle arg7)
|
void |
sendStickyBroadcast(Intent arg0)
|
void | |
void |
sendStickyOrderedBroadcast(Intent arg0, BroadcastReceiver arg1, Handler arg2, int arg3, String arg4, Bundle arg5)
|
void |
sendStickyOrderedBroadcastAsUser(Intent arg0, UserHandle arg1, BroadcastReceiver arg2, Handler arg3, int arg4, String arg5, Bundle arg6)
|
void |
setTheme(int arg0)
|
void |
setWallpaper(Bitmap arg0)
|
void |
setWallpaper(InputStream arg0)
|
void | |
void |
startActivities(Intent[] arg0)
|
void |
startActivity(Intent arg0)
|
void | |
boolean | |
void | |
void | |
ComponentName |
startService(Intent arg0)
|
boolean |
stopService(Intent arg0)
|
void |
unbindService(ServiceConnection arg0)
|
void |
unregisterReceiver(BroadcastReceiver arg0)
|
abstract boolean | |
boolean | |
abstract int |
checkCallingOrSelfPermission(String arg0)
|
abstract int |
checkCallingOrSelfUriPermission(Uri arg0, int arg1)
|
abstract int |
checkCallingPermission(String arg0)
|
abstract int |
checkCallingUriPermission(Uri arg0, int arg1)
|
abstract int |
checkPermission(String arg0, int arg1, int arg2)
|
abstract int |
checkSelfPermission(String arg0)
|
abstract int | |
abstract int |
checkUriPermission(Uri arg0, int arg1, int arg2, int arg3)
|
abstract void |
clearWallpaper()
|
abstract Context |
createConfigurationContext(Configuration arg0)
|
abstract Context |
createDisplayContext(Display arg0)
|
abstract Context |
createPackageContext(String arg0, int arg1)
|
abstract String[] |
databaseList()
|
abstract boolean |
deleteDatabase(String arg0)
|
abstract boolean |
deleteFile(String arg0)
|
abstract void | |
abstract void | |
abstract void | |
abstract void | |
abstract void | |
abstract void | |
abstract void | |
abstract String[] |
fileList()
|
abstract Context |
getApplicationContext()
|
abstract ApplicationInfo |
getApplicationInfo()
|
abstract AssetManager |
getAssets()
|
abstract File |
getCacheDir()
|
abstract ClassLoader |
getClassLoader()
|
abstract File |
getCodeCacheDir()
|
final int |
getColor(int arg0)
|
final ColorStateList |
getColorStateList(int arg0)
|
abstract ContentResolver |
getContentResolver()
|
abstract File |
getDatabasePath(String arg0)
|
abstract File |
getDir(String arg0, int arg1)
|
final Drawable |
getDrawable(int arg0)
|
abstract File |
getExternalCacheDir()
|
abstract File[] |
getExternalCacheDirs()
|
abstract File |
getExternalFilesDir(String arg0)
|
abstract File[] |
getExternalFilesDirs(String arg0)
|
abstract File[] |
getExternalMediaDirs()
|
abstract File |
getFileStreamPath(String arg0)
|
abstract File |
getFilesDir()
|
abstract Looper |
getMainLooper()
|
abstract File |
getNoBackupFilesDir()
|
abstract File |
getObbDir()
|
abstract File[] |
getObbDirs()
|
abstract String |
getPackageCodePath()
|
abstract PackageManager |
getPackageManager()
|
abstract String |
getPackageName()
|
abstract String |
getPackageResourcePath()
|
abstract Resources |
getResources()
|
abstract SharedPreferences |
getSharedPreferences(String arg0, int arg1)
|
final String |
getString(int arg0, Object... arg1)
|
final String |
getString(int arg0)
|
final <T> T |
getSystemService(Class<T> arg0)
|
abstract Object |
getSystemService(String arg0)
|
abstract String |
getSystemServiceName(Class<?> arg0)
|
final CharSequence |
getText(int arg0)
|
abstract Resources.Theme |
getTheme()
|
abstract Drawable |
getWallpaper()
|
abstract int |
getWallpaperDesiredMinimumHeight()
|
abstract int |
getWallpaperDesiredMinimumWidth()
|
abstract void | |
boolean |
isRestricted()
|
final TypedArray |
obtainStyledAttributes(AttributeSet arg0, int[] arg1)
|
final TypedArray |
obtainStyledAttributes(AttributeSet arg0, int[] arg1, int arg2, int arg3)
|
final TypedArray |
obtainStyledAttributes(int arg0, int[] arg1)
|
final TypedArray |
obtainStyledAttributes(int[] arg0)
|
abstract FileInputStream |
openFileInput(String arg0)
|
abstract FileOutputStream |
openFileOutput(String arg0, int arg1)
|
abstract SQLiteDatabase |
openOrCreateDatabase(String arg0, int arg1, SQLiteDatabase.CursorFactory arg2, DatabaseErrorHandler arg3)
|
abstract SQLiteDatabase | |
abstract Drawable |
peekWallpaper()
|
void |
registerComponentCallbacks(ComponentCallbacks arg0)
|
abstract Intent | |
abstract Intent | |
abstract void |
removeStickyBroadcast(Intent arg0)
|
abstract void | |
abstract void |
revokeUriPermission(Uri arg0, int arg1)
|
abstract void | |
abstract void | |
abstract void |
sendBroadcast(Intent arg0)
|
abstract void | |
abstract void | |
abstract void | |
abstract void | |
abstract void | |
abstract void |
sendOrderedBroadcastAsUser(Intent arg0, UserHandle arg1, String arg2, BroadcastReceiver arg3, Handler arg4, int arg5, String arg6, Bundle arg7)
|
abstract void |
sendStickyBroadcast(Intent arg0)
|
abstract void | |
abstract void |
sendStickyOrderedBroadcast(Intent arg0, BroadcastReceiver arg1, Handler arg2, int arg3, String arg4, Bundle arg5)
|
abstract void |
sendStickyOrderedBroadcastAsUser(Intent arg0, UserHandle arg1, BroadcastReceiver arg2, Handler arg3, int arg4, String arg5, Bundle arg6)
|
abstract void |
setTheme(int arg0)
|
abstract void |
setWallpaper(Bitmap arg0)
|
abstract void |
setWallpaper(InputStream arg0)
|
abstract void | |
abstract void |
startActivities(Intent[] arg0)
|
abstract void |
startActivity(Intent arg0)
|
abstract void | |
abstract boolean | |
abstract void | |
abstract void | |
abstract ComponentName |
startService(Intent arg0)
|
abstract boolean |
stopService(Intent arg0)
|
abstract void |
unbindService(ServiceConnection arg0)
|
void |
unregisterComponentCallbacks(ComponentCallbacks arg0)
|
abstract void |
unregisterReceiver(BroadcastReceiver arg0)
|
abstract void |
onTrimMemory(int arg0)
|
abstract void |
onConfigurationChanged(Configuration arg0)
|
abstract void |
onLowMemory()
|
Constants
public static final String SERVICE_ACTION_EXECUTE_TASK
Action broadcast by the GcmNetworkManager to the requesting package when a scheduled task is ready for execution.
public static final String SERVICE_ACTION_INITIALIZE
Action that a GcmTaskService
is started with when the service needs to initialize
its tasks.
public static final String SERVICE_PERMISSION
You must protect your service with this permission to avoid being bound to by an application other than Google Play Services.
Public Constructors
public GcmTaskService ()
Public Methods
public void onInitializeTasks ()
When your package is removed or updated, all of its network tasks are cleared by the GcmNetworkManager. You can override this method to reschedule them in the case of an updated package. This is not called when your application is first installed.
This is called on your application's main thread.
public abstract int onRunTask (TaskParams params)
Override this function to provide the logic for your task execution.
Parameters
params | Parameters provided at schedule time with
setTag(String) |
---|
Returns
- One of
RESULT_SUCCESS
,RESULT_RESCHEDULE
, orRESULT_FAILURE
.
public int onStartCommand (Intent intent, int flags, int startId)
Receives the command to begin doing work, for which it spawns another thread. If you override this, be sure your implementation calls super.onStartCommand(), otherwise your task will not be started correctly.