Class DownloadManager


  • public final class DownloadManager
    extends Object
    Manages downloads.

    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().

    • Field Detail

      • DEFAULT_MAX_PARALLEL_DOWNLOADS

        public static final int DEFAULT_MAX_PARALLEL_DOWNLOADS
        The default maximum number of parallel downloads.
        See Also:
        Constant Field Values
      • DEFAULT_MIN_RETRY_COUNT

        public static final int DEFAULT_MIN_RETRY_COUNT
        The default minimum number of times a download must be retried before failing.
        See Also:
        Constant Field Values
      • DEFAULT_REQUIREMENTS

        public static final Requirements DEFAULT_REQUIREMENTS
        The default requirement is that the device has network connectivity.
    • Method Detail

      • getApplicationLooper

        public Looper getApplicationLooper()
        Returns the Looper associated with the application thread that's used to access the manager, and on which the manager will call its Listeners.
      • isInitialized

        public boolean isInitialized()
        Returns whether the manager has completed initialization.
      • isIdle

        public boolean isIdle()
        Returns whether the manager is currently idle. The manager is idle if all downloads are in a terminal state (i.e. completed or failed), or if no progress can be made (e.g. because the download requirements are not met).
      • isWaitingForRequirements

        public 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. This is true if:
      • getRequirements

        public Requirements getRequirements()
        Returns the requirements needed to be met to progress.
      • getNotMetRequirements

        @RequirementFlags
        public int getNotMetRequirements()
        Returns the requirements needed for downloads to progress that are not currently met.
        Returns:
        The not met Requirements.RequirementFlags, or 0 if all requirements are met.
      • setRequirements

        public void setRequirements​(Requirements requirements)
        Sets the requirements that need to be met for downloads to progress.
        Parameters:
        requirements - A Requirements.
      • getMaxParallelDownloads

        public int getMaxParallelDownloads()
        Returns the maximum number of parallel downloads.
      • setMaxParallelDownloads

        public void setMaxParallelDownloads​(int maxParallelDownloads)
        Sets the maximum number of parallel downloads.
        Parameters:
        maxParallelDownloads - The maximum number of parallel downloads. Must be greater than 0.
      • getMinRetryCount

        public int getMinRetryCount()
        Returns the minimum number of times that a download will be retried. A download will fail if the specified number of retries is exceeded without any progress being made.
      • setMinRetryCount

        public void setMinRetryCount​(int minRetryCount)
        Sets the minimum number of times that a download will be retried. A download will fail if the specified number of retries is exceeded without any progress being made.
        Parameters:
        minRetryCount - The minimum number of times that a download will be retried.
      • getCurrentDownloads

        public List<Download> getCurrentDownloads()
        Returns current downloads. Downloads that are in terminal states (i.e. completed or failed) are not included. To query all downloads including those in terminal states, use getDownloadIndex() instead.
      • getDownloadsPaused

        public boolean getDownloadsPaused()
        Returns whether downloads are currently paused.
      • pauseDownloads

        public void pauseDownloads()
        Pauses downloads. Downloads that would otherwise be making progress will transition to Download.STATE_QUEUED.
      • setStopReason

        public void setStopReason​(@Nullable
                                  String id,
                                  int stopReason)
        Sets the stop reason for one or all downloads. To clear the stop reason, pass Download.STOP_REASON_NONE.
        Parameters:
        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.
      • addDownload

        public void addDownload​(DownloadRequest request)
        Adds a download defined by the given request.
        Parameters:
        request - The download request.
      • addDownload

        public void addDownload​(DownloadRequest request,
                                int stopReason)
        Adds a download defined by the given request and with the specified stop reason.
        Parameters:
        request - The download request.
        stopReason - An initial stop reason for the download, or Download.STOP_REASON_NONE if the download should be started.
      • removeDownload

        public void removeDownload​(String id)
        Cancels the download with the id and removes all downloaded data.
        Parameters:
        id - The unique content id of the download to be started.
      • removeAllDownloads

        public void removeAllDownloads()
        Cancels all pending downloads and removes all downloaded data.
      • release

        public void release()
        Stops the downloads and releases resources. Waits until the downloads are persisted to the download index. The manager must not be accessed after this method has been called.