Class DownloadService

  • All Implemented Interfaces:
    ComponentCallbacks, ComponentCallbacks2

    @Deprecated
    public abstract class DownloadService
    extends Service
    Deprecated.
    com.google.android.exoplayer2 is deprecated. Please migrate to androidx.media3 (which contains the same ExoPlayer code). See the migration guide for more details, including a script to help with the migration.
    A Service for downloading media.

    Apps with target SDK 33 and greater need to add the android.permission.POST_NOTIFICATIONS permission to the manifest and request the permission at runtime before starting downloads. Without that permission granted by the user, notifications posted by this service are not displayed. See the official UI guide for more detailed information.

    • Constructor Detail

      • DownloadService

        protected DownloadService​(int foregroundNotificationId)
        Deprecated.
        Creates a DownloadService.

        If foregroundNotificationId is FOREGROUND_NOTIFICATION_ID_NONE then the service will only ever run in the background, and no foreground notification will be displayed.

        If foregroundNotificationId is not FOREGROUND_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 by DEFAULT_FOREGROUND_NOTIFICATION_UPDATE_INTERVAL.

        Parameters:
        foregroundNotificationId - The notification id for the foreground notification, or FOREGROUND_NOTIFICATION_ID_NONE if the service should only ever run in the background.
      • DownloadService

        protected DownloadService​(int foregroundNotificationId,
                                  long foregroundNotificationUpdateInterval)
        Deprecated.
        Creates a DownloadService.
        Parameters:
        foregroundNotificationId - The notification id for the foreground notification, or FOREGROUND_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 if foregroundNotificationId is FOREGROUND_NOTIFICATION_ID_NONE.
      • DownloadService

        protected DownloadService​(int foregroundNotificationId,
                                  long foregroundNotificationUpdateInterval,
                                  @Nullable
                                  String channelId,
                                  @StringRes
                                  int channelNameResourceId,
                                  @StringRes
                                  int channelDescriptionResourceId)
        Deprecated.
        Creates a DownloadService.
        Parameters:
        foregroundNotificationId - The notification id for the foreground notification, or FOREGROUND_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 if foregroundNotificationId is FOREGROUND_NOTIFICATION_ID_NONE.
        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. The value may be truncated if it's too long. Ignored if foregroundNotificationId is FOREGROUND_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 if channelId is null or if foregroundNotificationId is FOREGROUND_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 if channelId is null or if foregroundNotificationId is FOREGROUND_NOTIFICATION_ID_NONE.
    • Method Detail

      • buildAddDownloadIntent

        public static Intent buildAddDownloadIntent​(Context context,
                                                    Class<? extends DownloadService> clazz,
                                                    DownloadRequest downloadRequest,
                                                    boolean foreground)
        Deprecated.
        Builds an Intent for adding a new download.
        Parameters:
        context - A Context.
        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)
        Deprecated.
        Builds an Intent for adding a new download.
        Parameters:
        context - A Context.
        clazz - The concrete download service being targeted by the intent.
        downloadRequest - The request to be executed.
        stopReason - An initial stop reason for the download, or Download.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)
        Deprecated.
        Builds an Intent for removing the download with the id.
        Parameters:
        context - A Context.
        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)
        Deprecated.
        Builds an Intent for removing all downloads.
        Parameters:
        context - A Context.
        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)
        Deprecated.
        Builds an Intent for resuming all downloads.
        Parameters:
        context - A Context.
        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)
        Deprecated.
        Builds an Intent to pause all downloads.
        Parameters:
        context - A Context.
        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)
        Deprecated.
        Builds an Intent for setting the stop reason for one or all downloads. To clear the stop reason, pass Download.STOP_REASON_NONE.
        Parameters:
        context - A Context.
        clazz - The concrete download service being targeted by the intent.
        id - The content id, or null 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)
        Deprecated.
        Builds an Intent for setting the requirements that need to be met for downloads to progress.
        Parameters:
        context - A Context.
        clazz - The concrete download service being targeted by the intent.
        requirements - A Requirements.
        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)
        Deprecated.
        Starts the service if not started already and adds a new download.
        Parameters:
        context - A Context.
        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)
        Deprecated.
        Starts the service if not started already and adds a new download.
        Parameters:
        context - A Context.
        clazz - The concrete download service to be started.
        downloadRequest - The request to be executed.
        stopReason - An initial stop reason for the download, or Download.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)
        Deprecated.
        Starts the service if not started already and removes a download.
        Parameters:
        context - A Context.
        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)
        Deprecated.
        Starts the service if not started already and removes all downloads.
        Parameters:
        context - A Context.
        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)
        Deprecated.
        Starts the service if not started already and resumes all downloads.
        Parameters:
        context - A Context.
        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)
        Deprecated.
        Starts the service if not started already and pauses all downloads.
        Parameters:
        context - A Context.
        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)
        Deprecated.
        Starts the service if not started already and sets the stop reason for one or all downloads. To clear stop reason, pass Download.STOP_REASON_NONE.
        Parameters:
        context - A Context.
        clazz - The concrete download service to be started.
        id - The content id, or null 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)
        Deprecated.
        Starts the service if not started already and sets the requirements that need to be met for downloads to progress.
        Parameters:
        context - A Context.
        clazz - The concrete download service to be started.
        requirements - A Requirements.
        foreground - Whether the service is started in the foreground.
      • startForeground

        public static void startForeground​(Context context,
                                           Class<? extends DownloadService> clazz)
        Deprecated.
        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 - A Context.
        clazz - The concrete download service to be started.
        See Also:
        start(Context, Class)
      • clearDownloadManagerHelpers

        public static void clearDownloadManagerHelpers()
        Deprecated.
        Clear all download manager helpers before restarting the service.

        Calling this method is normally only required if an app supports downloading content for multiple users for which different download directories should be used.

      • onCreate

        public void onCreate()
        Deprecated.
        Overrides:
        onCreate in class Service
      • onStartCommand

        public int onStartCommand​(@Nullable
                                  Intent intent,
                                  int flags,
                                  int startId)
        Deprecated.
        Overrides:
        onStartCommand in class Service
      • onDestroy

        public void onDestroy()
        Deprecated.
        Overrides:
        onDestroy in class Service
      • getDownloadManager

        protected abstract DownloadManager getDownloadManager()
        Deprecated.
        Returns a DownloadManager to be used to downloaded content. For each concrete download service subclass, this is called once in the lifecycle of the process when onCreate() is called on the first instance of the service. If the service is destroyed and a new instance is created later, the new instance will use the previously returned DownloadManager without this method being called again.
      • getScheduler

        @Nullable
        protected abstract Scheduler getScheduler()
        Deprecated.
        Returns a Scheduler 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 to FOREGROUND_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 returned Scheduler supports all of the requirements that have been specified for downloads to continue, then the service will stop itself and the Scheduler 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 a Scheduler that does not support 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.
      • getForegroundNotification

        protected abstract Notification getForegroundNotification​(List<Download> downloads,
                                                                  @RequirementFlags
                                                                  @com.google.android.exoplayer2.scheduler.Requirements.RequirementFlags int notMetRequirements)
        Deprecated.
        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 to FOREGROUND_NOTIFICATION_ID_NONE. This method is not called for such services, meaning it can be implemented to throw UnsupportedOperationException.

        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()
        Deprecated.
        Invalidates the current foreground notification and causes getForegroundNotification(List, int) to be invoked again if the service isn't stopped.