public final class DownloadManager extends Object
Normally a download manager should be accessed via a DownloadService. When a download
manager is used directly instead, downloads will be initially paused and so must be resumed by
calling resumeDownloads().
A download manager instance must be accessed only from the thread that created it, unless that
thread does not have a Looper. In that case, it must be accessed only from the
application's main thread. Registered listeners will be called on the same thread. In all cases
the `Looper` of the thread from which the manager must be accessed can be queried using getApplicationLooper().
| Modifier and Type | Class | Description |
|---|---|---|
static interface |
DownloadManager.Listener |
Listener for
DownloadManager events. |
| Modifier and Type | Field | Description |
|---|---|---|
static int |
DEFAULT_MAX_PARALLEL_DOWNLOADS |
The default maximum number of parallel downloads.
|
static int |
DEFAULT_MIN_RETRY_COUNT |
The default minimum number of times a download must be retried before failing.
|
static Requirements |
DEFAULT_REQUIREMENTS |
The default requirement is that the device has network connectivity.
|
| Constructor | Description |
|---|---|
DownloadManager(Context context,
DatabaseProvider databaseProvider,
Cache cache,
DataSource.Factory upstreamFactory) |
Deprecated.
|
DownloadManager(Context context,
DatabaseProvider databaseProvider,
Cache cache,
DataSource.Factory upstreamFactory,
Executor executor) |
Constructs a
DownloadManager. |
DownloadManager(Context context,
WritableDownloadIndex downloadIndex,
DownloaderFactory downloaderFactory) |
Constructs a
DownloadManager. |
| Modifier and Type | Method | Description |
|---|---|---|
void |
addDownload(DownloadRequest request) |
Adds a download defined by the given request.
|
void |
addDownload(DownloadRequest request,
int stopReason) |
Adds a download defined by the given request and with the specified stop reason.
|
void |
addListener(DownloadManager.Listener listener) |
Adds a
DownloadManager.Listener. |
Looper |
getApplicationLooper() |
|
List<Download> |
getCurrentDownloads() |
Returns current downloads.
|
DownloadIndex |
getDownloadIndex() |
Returns the used
DownloadIndex. |
boolean |
getDownloadsPaused() |
Returns whether downloads are currently paused.
|
int |
getMaxParallelDownloads() |
Returns the maximum number of parallel downloads.
|
int |
getMinRetryCount() |
Returns the minimum number of times that a download will be retried.
|
int |
getNotMetRequirements() |
Returns the requirements needed for downloads to progress that are not currently met.
|
Requirements |
getRequirements() |
Returns the requirements needed to be met to progress.
|
boolean |
isIdle() |
Returns whether the manager is currently idle.
|
boolean |
isInitialized() |
Returns whether the manager has completed initialization.
|
boolean |
isWaitingForRequirements() |
Returns whether this manager has one or more downloads that are not progressing for the sole
reason that the
Requirements are not met. |
void |
pauseDownloads() |
Pauses downloads.
|
void |
release() |
Stops the downloads and releases resources.
|
void |
removeAllDownloads() |
Cancels all pending downloads and removes all downloaded data.
|
void |
removeDownload(String id) |
Cancels the download with the
id and removes all downloaded data. |
void |
removeListener(DownloadManager.Listener listener) |
Removes a
DownloadManager.Listener. |
void |
resumeDownloads() |
Resumes downloads.
|
void |
setMaxParallelDownloads(int maxParallelDownloads) |
Sets the maximum number of parallel downloads.
|
void |
setMinRetryCount(int minRetryCount) |
Sets the minimum number of times that a download will be retried.
|
void |
setRequirements(Requirements requirements) |
Sets the requirements that need to be met for downloads to progress.
|
void |
setStopReason(String id,
int stopReason) |
Sets the stop reason for one or all downloads.
|
public static final int DEFAULT_MAX_PARALLEL_DOWNLOADS
public static final int DEFAULT_MIN_RETRY_COUNT
public static final Requirements DEFAULT_REQUIREMENTS
@Deprecated public DownloadManager(Context context, DatabaseProvider databaseProvider, Cache cache, DataSource.Factory upstreamFactory)
DownloadManager.context - Any context.databaseProvider - Provides the SQLite database in which downloads are persisted.cache - A cache to be used to store downloaded data. The cache should be configured with
an CacheEvictor that will not evict downloaded content, for example NoOpCacheEvictor.upstreamFactory - A DataSource.Factory for creating DataSources for downloading data.public DownloadManager(Context context, DatabaseProvider databaseProvider, Cache cache, DataSource.Factory upstreamFactory, Executor executor)
DownloadManager.context - Any context.databaseProvider - Provides the SQLite database in which downloads are persisted.cache - A cache to be used to store downloaded data. The cache should be configured with
an CacheEvictor that will not evict downloaded content, for example NoOpCacheEvictor.upstreamFactory - A DataSource.Factory for creating DataSources for downloading data.executor - An Executor used to download data. Passing Runnable::run will
cause each download task to download data on its own thread. Passing an Executor
that uses multiple threads will speed up download tasks that can be split into smaller
parts for parallel execution.public DownloadManager(Context context, WritableDownloadIndex downloadIndex, DownloaderFactory downloaderFactory)
DownloadManager.context - Any context.downloadIndex - The download index used to hold the download information.downloaderFactory - A factory for creating Downloaders.public Looper getApplicationLooper()
public boolean isInitialized()
public boolean isIdle()
public boolean isWaitingForRequirements()
Requirements are not met. This is true if:
Requirements are not met.
getDownloadsPaused() is false).
queued state.
public void addListener(DownloadManager.Listener listener)
DownloadManager.Listener.listener - The listener to be added.public void removeListener(DownloadManager.Listener listener)
DownloadManager.Listener.listener - The listener to be removed.public Requirements getRequirements()
@RequirementFlags public int getNotMetRequirements()
Requirements.RequirementFlags, or 0 if all requirements are met.public void setRequirements(Requirements requirements)
requirements - A Requirements.public int getMaxParallelDownloads()
public void setMaxParallelDownloads(int maxParallelDownloads)
maxParallelDownloads - The maximum number of parallel downloads. Must be greater than 0.public int getMinRetryCount()
public void setMinRetryCount(int minRetryCount)
minRetryCount - The minimum number of times that a download will be retried.public DownloadIndex getDownloadIndex()
DownloadIndex.public List<Download> getCurrentDownloads()
getDownloadIndex() instead.public boolean getDownloadsPaused()
public void resumeDownloads()
If the Requirements are met up to maxParallelDownloads will be started, excluding those with non-zero
stopReasons.
public void pauseDownloads()
Download.STATE_QUEUED.public void setStopReason(@Nullable
String id,
int stopReason)
Download.STOP_REASON_NONE.id - The content id of the download to update, or null to set the stop reason for
all downloads.stopReason - The stop reason, or Download.STOP_REASON_NONE.public void addDownload(DownloadRequest request)
request - The download request.public void addDownload(DownloadRequest request, int stopReason)
request - The download request.stopReason - An initial stop reason for the download, or Download.STOP_REASON_NONE
if the download should be started.public void removeDownload(String id)
id and removes all downloaded data.id - The unique content id of the download to be started.public void removeAllDownloads()
public void release()