public abstract class DownloadService
extends android.app.Service
Service for downloading media.| Modifier and Type | Field and Description |
|---|---|
static java.lang.String |
ACTION_ADD
Starts a download service, adding a new
DownloadAction to be executed. |
static java.lang.String |
ACTION_INIT
Starts a download service without adding a new
DownloadAction. |
static long |
DEFAULT_FOREGROUND_NOTIFICATION_UPDATE_INTERVAL
Default foreground notification update interval in milliseconds.
|
static java.lang.String |
KEY_DOWNLOAD_ACTION
Key for the
DownloadAction in an ACTION_ADD intent. |
static java.lang.String |
KEY_FOREGROUND
Key for a boolean flag in any intent to indicate whether the service was started in the
foreground.
|
START_CONTINUATION_MASK, START_FLAG_REDELIVERY, START_FLAG_RETRY, START_NOT_STICKY, START_REDELIVER_INTENT, START_STICKY, START_STICKY_COMPATIBILITY, STOP_FOREGROUND_DETACH, STOP_FOREGROUND_REMOVEACCESSIBILITY_SERVICE, ACCOUNT_SERVICE, ACTIVITY_SERVICE, ALARM_SERVICE, APP_OPS_SERVICE, APPWIDGET_SERVICE, AUDIO_SERVICE, BATTERY_SERVICE, BIND_ABOVE_CLIENT, BIND_ADJUST_WITH_ACTIVITY, BIND_ALLOW_OOM_MANAGEMENT, BIND_AUTO_CREATE, BIND_DEBUG_UNBIND, BIND_EXTERNAL_SERVICE, BIND_IMPORTANT, BIND_NOT_FOREGROUND, BIND_WAIVE_PRIORITY, BLUETOOTH_SERVICE, CAMERA_SERVICE, CAPTIONING_SERVICE, CARRIER_CONFIG_SERVICE, CLIPBOARD_SERVICE, COMPANION_DEVICE_SERVICE, CONNECTIVITY_SERVICE, CONSUMER_IR_SERVICE, CONTEXT_IGNORE_SECURITY, CONTEXT_INCLUDE_CODE, CONTEXT_RESTRICTED, DEVICE_POLICY_SERVICE, DISPLAY_SERVICE, DOWNLOAD_SERVICE, DROPBOX_SERVICE, FINGERPRINT_SERVICE, HARDWARE_PROPERTIES_SERVICE, INPUT_METHOD_SERVICE, INPUT_SERVICE, JOB_SCHEDULER_SERVICE, KEYGUARD_SERVICE, LAUNCHER_APPS_SERVICE, LAYOUT_INFLATER_SERVICE, LOCATION_SERVICE, MEDIA_PROJECTION_SERVICE, MEDIA_ROUTER_SERVICE, MEDIA_SESSION_SERVICE, MIDI_SERVICE, MODE_APPEND, MODE_ENABLE_WRITE_AHEAD_LOGGING, MODE_MULTI_PROCESS, MODE_NO_LOCALIZED_COLLATORS, MODE_PRIVATE, MODE_WORLD_READABLE, MODE_WORLD_WRITEABLE, NETWORK_STATS_SERVICE, NFC_SERVICE, NOTIFICATION_SERVICE, NSD_SERVICE, POWER_SERVICE, PRINT_SERVICE, RECEIVER_VISIBLE_TO_INSTANT_APPS, RESTRICTIONS_SERVICE, SEARCH_SERVICE, SENSOR_SERVICE, SHORTCUT_SERVICE, STORAGE_SERVICE, STORAGE_STATS_SERVICE, SYSTEM_HEALTH_SERVICE, TELECOM_SERVICE, TELEPHONY_SERVICE, TELEPHONY_SUBSCRIPTION_SERVICE, TEXT_CLASSIFICATION_SERVICE, TEXT_SERVICES_MANAGER_SERVICE, TV_INPUT_SERVICE, UI_MODE_SERVICE, USAGE_STATS_SERVICE, USB_SERVICE, USER_SERVICE, VIBRATOR_SERVICE, WALLPAPER_SERVICE, WIFI_AWARE_SERVICE, WIFI_P2P_SERVICE, WIFI_SERVICE, WINDOW_SERVICE| Modifier | Constructor and Description |
|---|---|
protected |
DownloadService(int foregroundNotificationId)
Creates a DownloadService with
DEFAULT_FOREGROUND_NOTIFICATION_UPDATE_INTERVAL. |
protected |
DownloadService(int foregroundNotificationId,
long foregroundNotificationUpdateInterval) |
protected |
DownloadService(int foregroundNotificationId,
long foregroundNotificationUpdateInterval,
java.lang.String channelId,
int channelName) |
| Modifier and Type | Method and Description |
|---|---|
static android.content.Intent |
buildAddActionIntent(android.content.Context context,
java.lang.Class<? extends DownloadService> clazz,
DownloadAction downloadAction,
boolean foreground)
Builds an
Intent for adding an action to be executed by the service. |
protected abstract DownloadManager |
getDownloadManager()
Returns a
DownloadManager to be used to downloaded content. |
protected abstract android.app.Notification |
getForegroundNotification(DownloadManager.TaskState[] taskStates)
Returns a notification to be displayed when this service running in the foreground.
|
protected Requirements |
getRequirements()
Returns requirements for downloads to take place.
|
protected abstract Scheduler |
getScheduler()
Returns a
Scheduler to restart the service when requirements allowing downloads to take
place are met. |
android.os.IBinder |
onBind(android.content.Intent intent) |
void |
onCreate() |
void |
onDestroy() |
int |
onStartCommand(android.content.Intent intent,
int flags,
int startId) |
void |
onTaskRemoved(android.content.Intent rootIntent) |
protected void |
onTaskStateChanged(DownloadManager.TaskState taskState)
Called when the state of a task changes.
|
static void |
start(android.content.Context context,
java.lang.Class<? extends DownloadService> clazz)
Starts the service without adding a new action.
|
static void |
startForeground(android.content.Context context,
java.lang.Class<? extends DownloadService> clazz)
Starts the service in the foreground without adding a new action.
|
static void |
startWithAction(android.content.Context context,
java.lang.Class<? extends DownloadService> clazz,
DownloadAction downloadAction,
boolean foreground)
Starts the service, adding an action to be executed.
|
dump, getApplication, onConfigurationChanged, onLowMemory, onRebind, onStart, onTrimMemory, onUnbind, startForeground, stopForeground, stopForeground, stopSelf, stopSelf, stopSelfResultattachBaseContext, bindService, checkCallingOrSelfPermission, checkCallingOrSelfUriPermission, checkCallingPermission, checkCallingUriPermission, checkPermission, checkSelfPermission, checkUriPermission, checkUriPermission, clearWallpaper, createConfigurationContext, createContextForSplit, createDeviceProtectedStorageContext, createDisplayContext, createPackageContext, databaseList, deleteDatabase, deleteFile, deleteSharedPreferences, enforceCallingOrSelfPermission, enforceCallingOrSelfUriPermission, enforceCallingPermission, enforceCallingUriPermission, enforcePermission, enforceUriPermission, enforceUriPermission, fileList, getApplicationContext, getApplicationInfo, getAssets, getBaseContext, getCacheDir, getClassLoader, getCodeCacheDir, getContentResolver, getDatabasePath, getDataDir, getDir, getExternalCacheDir, getExternalCacheDirs, getExternalFilesDir, getExternalFilesDirs, getExternalMediaDirs, getFilesDir, getFileStreamPath, getMainLooper, getNoBackupFilesDir, getObbDir, getObbDirs, getPackageCodePath, getPackageManager, getPackageName, getPackageResourcePath, getResources, getSharedPreferences, getSystemService, getSystemServiceName, getTheme, getWallpaper, getWallpaperDesiredMinimumHeight, getWallpaperDesiredMinimumWidth, grantUriPermission, isDeviceProtectedStorage, isRestricted, moveDatabaseFrom, moveSharedPreferencesFrom, openFileInput, openFileOutput, openOrCreateDatabase, openOrCreateDatabase, peekWallpaper, registerReceiver, registerReceiver, registerReceiver, registerReceiver, removeStickyBroadcast, removeStickyBroadcastAsUser, revokeUriPermission, revokeUriPermission, sendBroadcast, sendBroadcast, sendBroadcastAsUser, sendBroadcastAsUser, sendOrderedBroadcast, sendOrderedBroadcast, sendOrderedBroadcastAsUser, sendStickyBroadcast, sendStickyBroadcastAsUser, sendStickyOrderedBroadcast, sendStickyOrderedBroadcastAsUser, setTheme, setWallpaper, setWallpaper, startActivities, startActivities, startActivity, startActivity, startForegroundService, startInstrumentation, startIntentSender, startIntentSender, startService, stopService, unbindService, unregisterReceiverpublic static final java.lang.String ACTION_INIT
DownloadAction.public static final java.lang.String ACTION_ADD
DownloadAction to be executed.public static final java.lang.String KEY_DOWNLOAD_ACTION
DownloadAction in an ACTION_ADD intent.public static final java.lang.String KEY_FOREGROUND
Service.startForeground(int,
Notification).public static final long DEFAULT_FOREGROUND_NOTIFICATION_UPDATE_INTERVAL
protected DownloadService(int foregroundNotificationId)
DEFAULT_FOREGROUND_NOTIFICATION_UPDATE_INTERVAL.foregroundNotificationId - The notification id for the foreground notification, must not
be 0.protected DownloadService(int foregroundNotificationId,
long foregroundNotificationUpdateInterval)
foregroundNotificationId - The notification id for the foreground notification, must not
be 0.foregroundNotificationUpdateInterval - The maximum interval to update foreground
notification, in milliseconds.protected DownloadService(int foregroundNotificationId,
long foregroundNotificationUpdateInterval,
@Nullable
java.lang.String channelId,
@StringRes
int channelName)
foregroundNotificationId - The notification id for the foreground notification. Must not
be 0.foregroundNotificationUpdateInterval - The maximum interval between updates to the
foreground notification, in milliseconds.channelId - An id for a low priority notification channel to create, or null if
the app will take care of creating a notification channel if needed. If specified, must be
unique per package and the value may be truncated if it is too long.channelName - A string resource identifier for the user visible name of the channel, if
channelId is specified. The recommended maximum length is 40 characters; the value
may be truncated if it is too long.public static android.content.Intent buildAddActionIntent(android.content.Context context,
java.lang.Class<? extends DownloadService> clazz,
DownloadAction downloadAction,
boolean foreground)
Intent for adding an action to be executed by the service.context - A Context.clazz - The concrete download service being targeted by the intent.downloadAction - The action to be executed.foreground - Whether this intent will be used to start the service in the foreground.public static void startWithAction(android.content.Context context,
java.lang.Class<? extends DownloadService> clazz,
DownloadAction downloadAction,
boolean foreground)
context - A Context.clazz - The concrete download service to be started.downloadAction - The action to be executed.foreground - Whether the service is started in the foreground.public static void start(android.content.Context context,
java.lang.Class<? extends DownloadService> clazz)
context - A Context.clazz - The concrete download service to be started.startForeground(Context, Class)public static void startForeground(android.content.Context context,
java.lang.Class<? extends DownloadService> clazz)
context - A Context.clazz - The concrete download service to be started.start(Context, Class)public void onCreate()
onCreate in class android.app.Servicepublic int onStartCommand(android.content.Intent intent,
int flags,
int startId)
onStartCommand in class android.app.Servicepublic void onTaskRemoved(android.content.Intent rootIntent)
onTaskRemoved in class android.app.Servicepublic void onDestroy()
onDestroy in class android.app.Service@Nullable public android.os.IBinder onBind(android.content.Intent intent)
onBind in class android.app.Serviceprotected abstract DownloadManager getDownloadManager()
DownloadManager to be used to downloaded content. Called only once in the
life cycle of the service. The service will call DownloadManager.startDownloads() and
DownloadManager.stopDownloads() as necessary when requirements returned by getRequirements() are met or stop being met.@Nullable protected abstract Scheduler getScheduler()
Scheduler to restart the service when requirements allowing downloads to take
place are met. If null, the service will only be restarted if the process is still in
memory when the requirements are met.protected Requirements getRequirements()
protected abstract android.app.Notification getForegroundNotification(DownloadManager.TaskState[] taskStates)
This method is called when there is a task state change and periodically while there are
active tasks. The periodic update interval can be set using DownloadService(int,
long).
On API level 26 and above, this method may also be called just before the service stops,
with an empty taskStates array. The returned notification is used to satisfy system
requirements for foreground services.
taskStates - The states of all current tasks.protected void onTaskStateChanged(DownloadManager.TaskState taskState)
taskState - The state of the task.