Class DownloadService
- java.lang.Object
-
- android.content.Context
-
- android.content.ContextWrapper
-
- android.app.Service
-
- com.google.android.exoplayer2.offline.DownloadService
-
- All Implemented Interfaces:
ComponentCallbacks
,ComponentCallbacks2
public abstract class DownloadService extends Service
AService
for downloading media.
-
-
Field Summary
Fields Modifier and Type Field Description static String
ACTION_ADD_DOWNLOAD
Adds a new download.static String
ACTION_INIT
Starts a download service to resume any ongoing downloads.static String
ACTION_PAUSE_DOWNLOADS
Pauses all downloads.static String
ACTION_REMOVE_ALL_DOWNLOADS
Removes all downloads.static String
ACTION_REMOVE_DOWNLOAD
Removes a download.static String
ACTION_RESUME_DOWNLOADS
Resumes all downloads except those that have a non-zeroDownload.stopReason
.static String
ACTION_SET_REQUIREMENTS
Sets the requirements that need to be met for downloads to progress.static String
ACTION_SET_STOP_REASON
Sets the stop reason for one or all downloads.static long
DEFAULT_FOREGROUND_NOTIFICATION_UPDATE_INTERVAL
Default foreground notification update interval in milliseconds.static int
FOREGROUND_NOTIFICATION_ID_NONE
Invalid foreground notification id that can be used to run the service in the background.static String
KEY_CONTENT_ID
static String
KEY_DOWNLOAD_REQUEST
Key for theDownloadRequest
inACTION_ADD_DOWNLOAD
intents.static String
KEY_FOREGROUND
Key for a boolean extra that can be set on any intent to indicate whether the service was started in the foreground.static String
KEY_REQUIREMENTS
Key for theRequirements
inACTION_SET_REQUIREMENTS
intents.static String
KEY_STOP_REASON
Key for the integer stop reason inACTION_SET_STOP_REASON
andACTION_ADD_DOWNLOAD
intents.-
Fields inherited from class android.app.Service
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_REMOVE
-
Fields inherited from class android.content.Context
ACCESSIBILITY_SERVICE, ACCOUNT_SERVICE, ACTIVITY_SERVICE, ALARM_SERVICE, APP_OPS_SERVICE, APP_SEARCH_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_INCLUDE_CAPABILITIES, BIND_NOT_FOREGROUND, BIND_NOT_PERCEPTIBLE, BIND_WAIVE_PRIORITY, BIOMETRIC_SERVICE, BLOB_STORE_SERVICE, BLUETOOTH_SERVICE, BUGREPORT_SERVICE, CAMERA_SERVICE, CAPTIONING_SERVICE, CARRIER_CONFIG_SERVICE, CLIPBOARD_SERVICE, COMPANION_DEVICE_SERVICE, CONNECTIVITY_DIAGNOSTICS_SERVICE, CONNECTIVITY_SERVICE, CONSUMER_IR_SERVICE, CONTEXT_IGNORE_SECURITY, CONTEXT_INCLUDE_CODE, CONTEXT_RESTRICTED, CROSS_PROFILE_APPS_SERVICE, DEVICE_POLICY_SERVICE, DISPLAY_HASH_SERVICE, DISPLAY_SERVICE, DOMAIN_VERIFICATION_SERVICE, DOWNLOAD_SERVICE, DROPBOX_SERVICE, EUICC_SERVICE, FILE_INTEGRITY_SERVICE, FINGERPRINT_SERVICE, GAME_SERVICE, HARDWARE_PROPERTIES_SERVICE, INPUT_METHOD_SERVICE, INPUT_SERVICE, IPSEC_SERVICE, JOB_SCHEDULER_SERVICE, KEYGUARD_SERVICE, LAUNCHER_APPS_SERVICE, LAYOUT_INFLATER_SERVICE, LOCATION_SERVICE, MEDIA_COMMUNICATION_SERVICE, MEDIA_METRICS_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, PEOPLE_SERVICE, PERFORMANCE_HINT_SERVICE, POWER_SERVICE, PRINT_SERVICE, RECEIVER_VISIBLE_TO_INSTANT_APPS, RESTRICTIONS_SERVICE, ROLE_SERVICE, SEARCH_SERVICE, SENSOR_SERVICE, SHORTCUT_SERVICE, STORAGE_SERVICE, STORAGE_STATS_SERVICE, SYSTEM_HEALTH_SERVICE, TELECOM_SERVICE, TELEPHONY_IMS_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_MANAGER_SERVICE, VIBRATOR_SERVICE, VPN_MANAGEMENT_SERVICE, WALLPAPER_SERVICE, WIFI_AWARE_SERVICE, WIFI_P2P_SERVICE, WIFI_RTT_RANGING_SERVICE, WIFI_SERVICE, WINDOW_SERVICE
-
Fields inherited from interface android.content.ComponentCallbacks2
TRIM_MEMORY_BACKGROUND, TRIM_MEMORY_COMPLETE, TRIM_MEMORY_MODERATE, TRIM_MEMORY_RUNNING_CRITICAL, TRIM_MEMORY_RUNNING_LOW, TRIM_MEMORY_RUNNING_MODERATE, TRIM_MEMORY_UI_HIDDEN
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
DownloadService(int foregroundNotificationId)
Creates a DownloadService.protected
DownloadService(int foregroundNotificationId, long foregroundNotificationUpdateInterval)
Creates a DownloadService.protected
DownloadService(int foregroundNotificationId, long foregroundNotificationUpdateInterval, String channelId, int channelNameResourceId)
Deprecated.protected
DownloadService(int foregroundNotificationId, long foregroundNotificationUpdateInterval, String channelId, int channelNameResourceId, int channelDescriptionResourceId)
Creates a DownloadService.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description static Intent
buildAddDownloadIntent(Context context, Class<? extends DownloadService> clazz, DownloadRequest downloadRequest, boolean foreground)
Builds anIntent
for adding a new download.static Intent
buildAddDownloadIntent(Context context, Class<? extends DownloadService> clazz, DownloadRequest downloadRequest, int stopReason, boolean foreground)
Builds anIntent
for adding a new download.static Intent
buildPauseDownloadsIntent(Context context, Class<? extends DownloadService> clazz, boolean foreground)
Builds anIntent
to pause all downloads.static Intent
buildRemoveAllDownloadsIntent(Context context, Class<? extends DownloadService> clazz, boolean foreground)
Builds anIntent
for removing all downloads.static Intent
buildRemoveDownloadIntent(Context context, Class<? extends DownloadService> clazz, String id, boolean foreground)
Builds anIntent
for removing the download with theid
.static Intent
buildResumeDownloadsIntent(Context context, Class<? extends DownloadService> clazz, boolean foreground)
Builds anIntent
for resuming all downloads.static Intent
buildSetRequirementsIntent(Context context, Class<? extends DownloadService> clazz, Requirements requirements, boolean foreground)
Builds anIntent
for setting the requirements that need to be met for downloads to progress.static Intent
buildSetStopReasonIntent(Context context, Class<? extends DownloadService> clazz, String id, int stopReason, boolean foreground)
Builds anIntent
for setting the stop reason for one or all downloads.protected abstract DownloadManager
getDownloadManager()
Returns aDownloadManager
to be used to downloaded content.protected abstract Notification
getForegroundNotification(List<Download> downloads, @com.google.android.exoplayer2.scheduler.Requirements.RequirementFlags int notMetRequirements)
Returns a notification to be displayed when this service running in the foreground.protected abstract Scheduler
getScheduler()
Returns aScheduler
to restart the service when requirements for downloads to continue are met.protected void
invalidateForegroundNotification()
Invalidates the current foreground notification and causesgetForegroundNotification(List, int)
to be invoked again if the service isn't stopped.IBinder
onBind(Intent intent)
ThrowsUnsupportedOperationException
because this service is not designed to be bound.void
onCreate()
void
onDestroy()
int
onStartCommand(Intent intent, int flags, int startId)
void
onTaskRemoved(Intent rootIntent)
static void
sendAddDownload(Context context, Class<? extends DownloadService> clazz, DownloadRequest downloadRequest, boolean foreground)
Starts the service if not started already and adds a new download.static void
sendAddDownload(Context context, Class<? extends DownloadService> clazz, DownloadRequest downloadRequest, int stopReason, boolean foreground)
Starts the service if not started already and adds a new download.static void
sendPauseDownloads(Context context, Class<? extends DownloadService> clazz, boolean foreground)
Starts the service if not started already and pauses all downloads.static void
sendRemoveAllDownloads(Context context, Class<? extends DownloadService> clazz, boolean foreground)
Starts the service if not started already and removes all downloads.static void
sendRemoveDownload(Context context, Class<? extends DownloadService> clazz, String id, boolean foreground)
Starts the service if not started already and removes a download.static void
sendResumeDownloads(Context context, Class<? extends DownloadService> clazz, boolean foreground)
Starts the service if not started already and resumes all downloads.static void
sendSetRequirements(Context context, Class<? extends DownloadService> clazz, Requirements requirements, boolean foreground)
Starts the service if not started already and sets the requirements that need to be met for downloads to progress.static void
sendSetStopReason(Context context, Class<? extends DownloadService> clazz, String id, int stopReason, boolean foreground)
Starts the service if not started already and sets the stop reason for one or all downloads.static void
start(Context context, Class<? extends DownloadService> clazz)
Starts a download service to resume any ongoing downloads.static void
startForeground(Context context, Class<? extends DownloadService> clazz)
Starts the service in the foreground without adding a new download request.-
Methods inherited from class android.app.Service
attachBaseContext, dump, getApplication, getForegroundServiceType, onConfigurationChanged, onLowMemory, onRebind, onStart, onTrimMemory, onUnbind, startForeground, startForeground, stopForeground, stopForeground, stopSelf, stopSelf, stopSelfResult
-
Methods inherited from class android.content.ContextWrapper
bindIsolatedService, bindService, bindService, bindServiceAsUser, checkCallingOrSelfPermission, checkCallingOrSelfUriPermission, checkCallingOrSelfUriPermissions, checkCallingPermission, checkCallingUriPermission, checkCallingUriPermissions, checkPermission, checkSelfPermission, checkUriPermission, checkUriPermission, checkUriPermissions, clearWallpaper, createAttributionContext, createConfigurationContext, createContext, createContextForSplit, createDeviceProtectedStorageContext, createDisplayContext, createPackageContext, createWindowContext, createWindowContext, databaseList, deleteDatabase, deleteFile, deleteSharedPreferences, enforceCallingOrSelfPermission, enforceCallingOrSelfUriPermission, enforceCallingPermission, enforceCallingUriPermission, enforcePermission, enforceUriPermission, enforceUriPermission, fileList, getApplicationContext, getApplicationInfo, getAssets, getAttributionSource, getAttributionTag, getBaseContext, getCacheDir, getClassLoader, getCodeCacheDir, getContentResolver, getDatabasePath, getDataDir, getDir, getDisplay, getExternalCacheDir, getExternalCacheDirs, getExternalFilesDir, getExternalFilesDirs, getExternalMediaDirs, getFilesDir, getFileStreamPath, getMainExecutor, getMainLooper, getNoBackupFilesDir, getObbDir, getObbDirs, getOpPackageName, getPackageCodePath, getPackageManager, getPackageName, getPackageResourcePath, getParams, getResources, getSharedPreferences, getSystemService, getSystemServiceName, getTheme, getWallpaper, getWallpaperDesiredMinimumHeight, getWallpaperDesiredMinimumWidth, grantUriPermission, isDeviceProtectedStorage, isRestricted, isUiContext, moveDatabaseFrom, moveSharedPreferencesFrom, openFileInput, openFileOutput, openOrCreateDatabase, openOrCreateDatabase, peekWallpaper, registerReceiver, registerReceiver, registerReceiver, registerReceiver, removeStickyBroadcast, removeStickyBroadcastAsUser, revokeUriPermission, revokeUriPermission, sendBroadcast, sendBroadcast, sendBroadcastAsUser, sendBroadcastAsUser, sendOrderedBroadcast, sendOrderedBroadcast, sendOrderedBroadcast, sendOrderedBroadcast, sendOrderedBroadcastAsUser, sendStickyBroadcast, sendStickyBroadcast, sendStickyBroadcastAsUser, sendStickyOrderedBroadcast, sendStickyOrderedBroadcastAsUser, setTheme, setWallpaper, setWallpaper, startActivities, startActivities, startActivity, startActivity, startForegroundService, startInstrumentation, startIntentSender, startIntentSender, startService, stopService, unbindService, unregisterReceiver, updateServiceGroup
-
Methods inherited from class android.content.Context
getColor, getColorStateList, getDrawable, getString, getString, getSystemService, getText, obtainStyledAttributes, obtainStyledAttributes, obtainStyledAttributes, obtainStyledAttributes, registerComponentCallbacks, sendBroadcastWithMultiplePermissions, unregisterComponentCallbacks
-
-
-
-
Field Detail
-
ACTION_INIT
public static final String ACTION_INIT
Starts a download service to resume any ongoing downloads. Extras:KEY_FOREGROUND
- SeeKEY_FOREGROUND
.
- See Also:
- Constant Field Values
-
ACTION_ADD_DOWNLOAD
public static final String ACTION_ADD_DOWNLOAD
Adds a new download. Extras:KEY_DOWNLOAD_REQUEST
- ADownloadRequest
defining the download to be added.KEY_STOP_REASON
- An initial stop reason for the download. If omittedDownload.STOP_REASON_NONE
is used.KEY_FOREGROUND
- SeeKEY_FOREGROUND
.
- See Also:
- Constant Field Values
-
ACTION_REMOVE_DOWNLOAD
public static final String ACTION_REMOVE_DOWNLOAD
Removes a download. Extras:KEY_CONTENT_ID
- The content id of a download to remove.KEY_FOREGROUND
- SeeKEY_FOREGROUND
.
- See Also:
- Constant Field Values
-
ACTION_REMOVE_ALL_DOWNLOADS
public static final String ACTION_REMOVE_ALL_DOWNLOADS
Removes all downloads. Extras:KEY_FOREGROUND
- SeeKEY_FOREGROUND
.
- See Also:
- Constant Field Values
-
ACTION_RESUME_DOWNLOADS
public static final String ACTION_RESUME_DOWNLOADS
Resumes all downloads except those that have a non-zeroDownload.stopReason
. Extras:KEY_FOREGROUND
- SeeKEY_FOREGROUND
.
- See Also:
- Constant Field Values
-
ACTION_PAUSE_DOWNLOADS
public static final String ACTION_PAUSE_DOWNLOADS
Pauses all downloads. Extras:KEY_FOREGROUND
- SeeKEY_FOREGROUND
.
- See Also:
- Constant Field Values
-
ACTION_SET_STOP_REASON
public static final String ACTION_SET_STOP_REASON
Sets the stop reason for one or all downloads. To clear the stop reason, passDownload.STOP_REASON_NONE
. Extras:KEY_CONTENT_ID
- The content id of a single download to update with the stop reason. If omitted, all downloads will be updated.KEY_STOP_REASON
- An application provided reason for stopping the download or downloads, orDownload.STOP_REASON_NONE
to clear the stop reason.KEY_FOREGROUND
- SeeKEY_FOREGROUND
.
- See Also:
- Constant Field Values
-
ACTION_SET_REQUIREMENTS
public static final String ACTION_SET_REQUIREMENTS
Sets the requirements that need to be met for downloads to progress. Extras:KEY_REQUIREMENTS
- ARequirements
.KEY_FOREGROUND
- SeeKEY_FOREGROUND
.
- See Also:
- Constant Field Values
-
KEY_DOWNLOAD_REQUEST
public static final String KEY_DOWNLOAD_REQUEST
Key for theDownloadRequest
inACTION_ADD_DOWNLOAD
intents.- See Also:
- Constant Field Values
-
KEY_CONTENT_ID
public static final String KEY_CONTENT_ID
- See Also:
- Constant Field Values
-
KEY_STOP_REASON
public static final String KEY_STOP_REASON
Key for the integer stop reason inACTION_SET_STOP_REASON
andACTION_ADD_DOWNLOAD
intents.- See Also:
- Constant Field Values
-
KEY_REQUIREMENTS
public static final String KEY_REQUIREMENTS
Key for theRequirements
inACTION_SET_REQUIREMENTS
intents.- See Also:
- Constant Field Values
-
KEY_FOREGROUND
public static final String KEY_FOREGROUND
Key for a boolean extra that can be set on any intent to indicate whether the service was started in the foreground. If set, the service is guaranteed to callService.startForeground(int, Notification)
.- See Also:
- Constant Field Values
-
FOREGROUND_NOTIFICATION_ID_NONE
public static final int FOREGROUND_NOTIFICATION_ID_NONE
Invalid foreground notification id that can be used to run the service in the background.- See Also:
- Constant Field Values
-
DEFAULT_FOREGROUND_NOTIFICATION_UPDATE_INTERVAL
public static final long DEFAULT_FOREGROUND_NOTIFICATION_UPDATE_INTERVAL
Default foreground notification update interval in milliseconds.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
DownloadService
protected DownloadService(int foregroundNotificationId)
Creates a DownloadService.If
foregroundNotificationId
isFOREGROUND_NOTIFICATION_ID_NONE
then the service will only ever run in the background, and no foreground notification will be displayed.If
foregroundNotificationId
is notFOREGROUND_NOTIFICATION_ID_NONE
then the service will run in the foreground. The foreground notification will be updated at least as often as the interval specified byDEFAULT_FOREGROUND_NOTIFICATION_UPDATE_INTERVAL
.- Parameters:
foregroundNotificationId
- The notification id for the foreground notification, orFOREGROUND_NOTIFICATION_ID_NONE
if the service should only ever run in the background.
-
DownloadService
protected DownloadService(int foregroundNotificationId, long foregroundNotificationUpdateInterval)
Creates a DownloadService.- Parameters:
foregroundNotificationId
- The notification id for the foreground notification, orFOREGROUND_NOTIFICATION_ID_NONE
if the service should only ever run in the background.foregroundNotificationUpdateInterval
- The maximum interval between updates to the foreground notification, in milliseconds. Ignored ifforegroundNotificationId
isFOREGROUND_NOTIFICATION_ID_NONE
.
-
DownloadService
@Deprecated protected DownloadService(int foregroundNotificationId, long foregroundNotificationUpdateInterval, @Nullable String channelId, @StringRes int channelNameResourceId)
Deprecated.
-
DownloadService
protected DownloadService(int foregroundNotificationId, long foregroundNotificationUpdateInterval, @Nullable String channelId, @StringRes int channelNameResourceId, @StringRes int channelDescriptionResourceId)
Creates a DownloadService.- Parameters:
foregroundNotificationId
- The notification id for the foreground notification, orFOREGROUND_NOTIFICATION_ID_NONE
if the service should only ever run in the background.foregroundNotificationUpdateInterval
- The maximum interval between updates to the foreground notification, in milliseconds. Ignored ifforegroundNotificationId
isFOREGROUND_NOTIFICATION_ID_NONE
.channelId
- An id for a low priority notification channel to create, ornull
if the app will take care of creating a notification channel if needed. If specified, must be unique per package. The value may be truncated if it's too long. Ignored ifforegroundNotificationId
isFOREGROUND_NOTIFICATION_ID_NONE
.channelNameResourceId
- A string resource identifier for the user visible name of the notification channel. The recommended maximum length is 40 characters. The value may be truncated if it's too long. Ignored ifchannelId
is null or ifforegroundNotificationId
isFOREGROUND_NOTIFICATION_ID_NONE
.channelDescriptionResourceId
- A string resource identifier for the user visible description of the notification channel, or 0 if no description is provided. The recommended maximum length is 300 characters. The value may be truncated if it is too long. Ignored ifchannelId
is null or ifforegroundNotificationId
isFOREGROUND_NOTIFICATION_ID_NONE
.
-
-
Method Detail
-
buildAddDownloadIntent
public static Intent buildAddDownloadIntent(Context context, Class<? extends DownloadService> clazz, DownloadRequest downloadRequest, boolean foreground)
Builds anIntent
for adding a new download.- Parameters:
context
- AContext
.clazz
- The concrete download service being targeted by the intent.downloadRequest
- The request to be executed.foreground
- Whether this intent will be used to start the service in the foreground.- Returns:
- The created intent.
-
buildAddDownloadIntent
public static Intent buildAddDownloadIntent(Context context, Class<? extends DownloadService> clazz, DownloadRequest downloadRequest, int stopReason, boolean foreground)
Builds anIntent
for adding a new download.- Parameters:
context
- AContext
.clazz
- The concrete download service being targeted by the intent.downloadRequest
- The request to be executed.stopReason
- An initial stop reason for the download, orDownload.STOP_REASON_NONE
if the download should be started.foreground
- Whether this intent will be used to start the service in the foreground.- Returns:
- The created intent.
-
buildRemoveDownloadIntent
public static Intent buildRemoveDownloadIntent(Context context, Class<? extends DownloadService> clazz, String id, boolean foreground)
Builds anIntent
for removing the download with theid
.- Parameters:
context
- AContext
.clazz
- The concrete download service being targeted by the intent.id
- The content id.foreground
- Whether this intent will be used to start the service in the foreground.- Returns:
- The created intent.
-
buildRemoveAllDownloadsIntent
public static Intent buildRemoveAllDownloadsIntent(Context context, Class<? extends DownloadService> clazz, boolean foreground)
Builds anIntent
for removing all downloads.- Parameters:
context
- AContext
.clazz
- The concrete download service being targeted by the intent.foreground
- Whether this intent will be used to start the service in the foreground.- Returns:
- The created intent.
-
buildResumeDownloadsIntent
public static Intent buildResumeDownloadsIntent(Context context, Class<? extends DownloadService> clazz, boolean foreground)
Builds anIntent
for resuming all downloads.- Parameters:
context
- AContext
.clazz
- The concrete download service being targeted by the intent.foreground
- Whether this intent will be used to start the service in the foreground.- Returns:
- The created intent.
-
buildPauseDownloadsIntent
public static Intent buildPauseDownloadsIntent(Context context, Class<? extends DownloadService> clazz, boolean foreground)
Builds anIntent
to pause all downloads.- Parameters:
context
- AContext
.clazz
- The concrete download service being targeted by the intent.foreground
- Whether this intent will be used to start the service in the foreground.- Returns:
- The created intent.
-
buildSetStopReasonIntent
public static Intent buildSetStopReasonIntent(Context context, Class<? extends DownloadService> clazz, @Nullable String id, int stopReason, boolean foreground)
Builds anIntent
for setting the stop reason for one or all downloads. To clear the stop reason, passDownload.STOP_REASON_NONE
.- Parameters:
context
- AContext
.clazz
- The concrete download service being targeted by the intent.id
- The content id, ornull
to set the stop reason for all downloads.stopReason
- An application defined stop reason.foreground
- Whether this intent will be used to start the service in the foreground.- Returns:
- The created intent.
-
buildSetRequirementsIntent
public static Intent buildSetRequirementsIntent(Context context, Class<? extends DownloadService> clazz, Requirements requirements, boolean foreground)
Builds anIntent
for setting the requirements that need to be met for downloads to progress.- Parameters:
context
- AContext
.clazz
- The concrete download service being targeted by the intent.requirements
- ARequirements
.foreground
- Whether this intent will be used to start the service in the foreground.- Returns:
- The created intent.
-
sendAddDownload
public static void sendAddDownload(Context context, Class<? extends DownloadService> clazz, DownloadRequest downloadRequest, boolean foreground)
Starts the service if not started already and adds a new download.- Parameters:
context
- AContext
.clazz
- The concrete download service to be started.downloadRequest
- The request to be executed.foreground
- Whether the service is started in the foreground.
-
sendAddDownload
public static void sendAddDownload(Context context, Class<? extends DownloadService> clazz, DownloadRequest downloadRequest, int stopReason, boolean foreground)
Starts the service if not started already and adds a new download.- Parameters:
context
- AContext
.clazz
- The concrete download service to be started.downloadRequest
- The request to be executed.stopReason
- An initial stop reason for the download, orDownload.STOP_REASON_NONE
if the download should be started.foreground
- Whether the service is started in the foreground.
-
sendRemoveDownload
public static void sendRemoveDownload(Context context, Class<? extends DownloadService> clazz, String id, boolean foreground)
Starts the service if not started already and removes a download.- Parameters:
context
- AContext
.clazz
- The concrete download service to be started.id
- The content id.foreground
- Whether the service is started in the foreground.
-
sendRemoveAllDownloads
public static void sendRemoveAllDownloads(Context context, Class<? extends DownloadService> clazz, boolean foreground)
Starts the service if not started already and removes all downloads.- Parameters:
context
- AContext
.clazz
- The concrete download service to be started.foreground
- Whether the service is started in the foreground.
-
sendResumeDownloads
public static void sendResumeDownloads(Context context, Class<? extends DownloadService> clazz, boolean foreground)
Starts the service if not started already and resumes all downloads.- Parameters:
context
- AContext
.clazz
- The concrete download service to be started.foreground
- Whether the service is started in the foreground.
-
sendPauseDownloads
public static void sendPauseDownloads(Context context, Class<? extends DownloadService> clazz, boolean foreground)
Starts the service if not started already and pauses all downloads.- Parameters:
context
- AContext
.clazz
- The concrete download service to be started.foreground
- Whether the service is started in the foreground.
-
sendSetStopReason
public static void sendSetStopReason(Context context, Class<? extends DownloadService> clazz, @Nullable String id, int stopReason, boolean foreground)
Starts the service if not started already and sets the stop reason for one or all downloads. To clear stop reason, passDownload.STOP_REASON_NONE
.- Parameters:
context
- AContext
.clazz
- The concrete download service to be started.id
- The content id, ornull
to set the stop reason for all downloads.stopReason
- An application defined stop reason.foreground
- Whether the service is started in the foreground.
-
sendSetRequirements
public static void sendSetRequirements(Context context, Class<? extends DownloadService> clazz, Requirements requirements, boolean foreground)
Starts the service if not started already and sets the requirements that need to be met for downloads to progress.- Parameters:
context
- AContext
.clazz
- The concrete download service to be started.requirements
- ARequirements
.foreground
- Whether the service is started in the foreground.
-
start
public static void start(Context context, Class<? extends DownloadService> clazz)
Starts a download service to resume any ongoing downloads.- Parameters:
context
- AContext
.clazz
- The concrete download service to be started.- See Also:
startForeground(Context, Class)
-
startForeground
public static void startForeground(Context context, Class<? extends DownloadService> clazz)
Starts the service in the foreground without adding a new download request. If there are any not finished downloads and the requirements are met, the service resumes downloading. Otherwise it stops immediately.- Parameters:
context
- AContext
.clazz
- The concrete download service to be started.- See Also:
start(Context, Class)
-
onStartCommand
public int onStartCommand(@Nullable Intent intent, int flags, int startId)
- Overrides:
onStartCommand
in classService
-
onTaskRemoved
public void onTaskRemoved(Intent rootIntent)
- Overrides:
onTaskRemoved
in classService
-
onBind
@Nullable public final IBinder onBind(Intent intent)
ThrowsUnsupportedOperationException
because this service is not designed to be bound.
-
getDownloadManager
protected abstract DownloadManager getDownloadManager()
Returns aDownloadManager
to be used to downloaded content. Called only once in the life cycle of the process.
-
getScheduler
@Nullable protected abstract Scheduler getScheduler()
Returns aScheduler
to restart the service when requirements for downloads to continue are met.This method is not called on all devices or for all service configurations. When it is called, it's called only once in the life cycle of the process. If a service has unfinished downloads that cannot make progress due to unmet requirements, it will behave according to the first matching case below:
- If the service has
foregroundNotificationId
set toFOREGROUND_NOTIFICATION_ID_NONE
, then this method will not be called. The service will remain in the background until the downloads are able to continue to completion or the service is killed by the platform. - If the device API level is less than 31, a
Scheduler
is returned from this method, and the returnedScheduler
supports
all of the requirements that have been specified for downloads to continue, then the service will stop itself and theScheduler
will be used to restart it in the foreground when the requirements are met. - If the device API level is less than 31 and either
null
or aScheduler
that does notsupport
all of the requirements is returned from this method, then the service will remain in the foreground until the downloads are able to continue to completion. - If the device API level is 31 or above, then this method will not be called and the
service will remain in the foreground until the downloads are able to continue to
completion. A
Scheduler
cannot be used for this case due to Android 12 foreground service launch restrictions.
- If the service has
-
getForegroundNotification
protected abstract Notification getForegroundNotification(List<Download> downloads, @RequirementFlags @com.google.android.exoplayer2.scheduler.Requirements.RequirementFlags int notMetRequirements)
Returns a notification to be displayed when this service running in the foreground.Download services that do not wish to run in the foreground should be created by setting the
foregroundNotificationId
constructor argument toFOREGROUND_NOTIFICATION_ID_NONE
. This method is not called for such services, meaning it can be implemented to throwUnsupportedOperationException
.- Parameters:
downloads
- The current downloads.notMetRequirements
- Any requirements for downloads that are not currently met.- Returns:
- The foreground notification to display.
-
invalidateForegroundNotification
protected final void invalidateForegroundNotification()
Invalidates the current foreground notification and causesgetForegroundNotification(List, int)
to be invoked again if the service isn't stopped.
-
-