DownloadService


@UnstableApi
public abstract class DownloadService extends Service


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.

Summary

Constants

static final String
ACTION_ADD_DOWNLOAD = "androidx.media3.exoplayer.downloadService.action.ADD_DOWNLOAD"

Adds a new download.

static final String
ACTION_INIT = "androidx.media3.exoplayer.downloadService.action.INIT"

Starts a download service to resume any ongoing downloads.

static final String
ACTION_PAUSE_DOWNLOADS = "androidx.media3.exoplayer.downloadService.action.PAUSE_DOWNLOADS"

Pauses all downloads.

static final String
ACTION_REMOVE_ALL_DOWNLOADS = "androidx.media3.exoplayer.downloadService.action.REMOVE_ALL_DOWNLOADS"

Removes all downloads.

static final String
ACTION_REMOVE_DOWNLOAD = "androidx.media3.exoplayer.downloadService.action.REMOVE_DOWNLOAD"

Removes a download.

static final String
ACTION_RESUME_DOWNLOADS = "androidx.media3.exoplayer.downloadService.action.RESUME_DOWNLOADS"

Resumes all downloads except those that have a non-zero stopReason.

static final String
ACTION_SET_REQUIREMENTS = "androidx.media3.exoplayer.downloadService.action.SET_REQUIREMENTS"

Sets the requirements that need to be met for downloads to progress.

static final String
ACTION_SET_STOP_REASON = "androidx.media3.exoplayer.downloadService.action.SET_STOP_REASON"

Sets the stop reason for one or all downloads.

static final long

Default foreground notification update interval in milliseconds.

static final int

Invalid foreground notification id that can be used to run the service in the background.

static final String
KEY_CONTENT_ID = "content_id"

Key for the String content id in ACTION_SET_STOP_REASON and ACTION_REMOVE_DOWNLOAD intents.

static final String
KEY_DOWNLOAD_REQUEST = "download_request"

Key for the DownloadRequest in ACTION_ADD_DOWNLOAD intents.

static final String
KEY_FOREGROUND = "foreground"

Key for a boolean extra that can be set on any intent to indicate whether the service was started in the foreground.

static final String
KEY_REQUIREMENTS = "requirements"

Key for the Requirements in ACTION_SET_REQUIREMENTS intents.

static final String
KEY_STOP_REASON = "stop_reason"

Key for the integer stop reason in ACTION_SET_STOP_REASON and ACTION_ADD_DOWNLOAD intents.

Protected constructors

DownloadService(int foregroundNotificationId)

Creates a DownloadService.

DownloadService(
    int foregroundNotificationId,
    long foregroundNotificationUpdateInterval
)

Creates a DownloadService.

DownloadService(
    int foregroundNotificationId,
    long foregroundNotificationUpdateInterval,
    @Nullable String channelId,
    @StringRes int channelNameResourceId,
    @StringRes int channelDescriptionResourceId
)

Creates a DownloadService.

Public methods

static Intent
buildAddDownloadIntent(
    Context context,
    Class<DownloadService> clazz,
    DownloadRequest downloadRequest,
    boolean foreground
)

Builds an Intent for adding a new download.

static Intent
buildAddDownloadIntent(
    Context context,
    Class<DownloadService> clazz,
    DownloadRequest downloadRequest,
    int stopReason,
    boolean foreground
)

Builds an Intent for adding a new download.

static Intent
buildPauseDownloadsIntent(
    Context context,
    Class<DownloadService> clazz,
    boolean foreground
)

Builds an Intent to pause all downloads.

static Intent
buildRemoveAllDownloadsIntent(
    Context context,
    Class<DownloadService> clazz,
    boolean foreground
)

Builds an Intent for removing all downloads.

static Intent
buildRemoveDownloadIntent(
    Context context,
    Class<DownloadService> clazz,
    String id,
    boolean foreground
)

Builds an Intent for removing the download with the id.

static Intent
buildResumeDownloadsIntent(
    Context context,
    Class<DownloadService> clazz,
    boolean foreground
)

Builds an Intent for resuming all downloads.

static Intent
buildSetRequirementsIntent(
    Context context,
    Class<DownloadService> clazz,
    Requirements requirements,
    boolean foreground
)

Builds an Intent for setting the requirements that need to be met for downloads to progress.

static Intent
buildSetStopReasonIntent(
    Context context,
    Class<DownloadService> clazz,
    @Nullable String id,
    int stopReason,
    boolean foreground
)

Builds an Intent for setting the stop reason for one or all downloads.

static void

Clear all download manager helpers before restarting the service.

final @Nullable IBinder
onBind(Intent intent)

Throws UnsupportedOperationException because this service is not designed to be bound.

void
void
int
onStartCommand(@Nullable Intent intent, int flags, int startId)
void
onTaskRemoved(Intent rootIntent)
static void
sendAddDownload(
    Context context,
    Class<DownloadService> clazz,
    DownloadRequest downloadRequest,
    boolean foreground
)

Starts the service if not started already and adds a new download.

static void
sendAddDownload(
    Context context,
    Class<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<DownloadService> clazz,
    boolean foreground
)

Starts the service if not started already and pauses all downloads.

static void
sendRemoveAllDownloads(
    Context context,
    Class<DownloadService> clazz,
    boolean foreground
)

Starts the service if not started already and removes all downloads.

static void
sendRemoveDownload(
    Context context,
    Class<DownloadService> clazz,
    String id,
    boolean foreground
)

Starts the service if not started already and removes a download.

static void
sendResumeDownloads(
    Context context,
    Class<DownloadService> clazz,
    boolean foreground
)

Starts the service if not started already and resumes all downloads.

static void
sendSetRequirements(
    Context context,
    Class<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<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.

static void
start(Context context, Class<DownloadService> clazz)

Starts a download service to resume any ongoing downloads.

static void

Starts the service in the foreground without adding a new download request.

Protected methods

abstract DownloadManager

Returns a DownloadManager to be used to downloaded content.

abstract Notification
getForegroundNotification(
    List<Download> downloads,
    @Requirements.RequirementFlags int notMetRequirements
)

Returns a notification to be displayed when this service running in the foreground.

abstract @Nullable Scheduler

Returns a Scheduler to restart the service when requirements for downloads to continue are met.

final void

Invalidates the current foreground notification and causes getForegroundNotification to be invoked again if the service isn't stopped.

Inherited Constants

From android.content.ComponentCallbacks2
static final int
static final int
static final int
static final int
static final int
static final int
static final int
From android.content.Context
static final String
ACCESSIBILITY_SERVICE = "accessibility"
static final String
ACCOUNT_SERVICE = "account"
static final String
ACTIVITY_SERVICE = "activity"
static final String
ALARM_SERVICE = "alarm"
static final String
APPWIDGET_SERVICE = "appwidget"
static final String
APP_OPS_SERVICE = "appops"
static final String
APP_SEARCH_SERVICE = "app_search"
static final String
AUDIO_SERVICE = "audio"
static final String
BATTERY_SERVICE = "batterymanager"
static final int
static final int
static final int
static final int
static final int
static final int
static final int
BIND_EXTERNAL_SERVICE = -2147483648
static final long
BIND_EXTERNAL_SERVICE_LONG = 4611686018427387904
static final int
static final int
static final int
static final int
static final int
static final int
static final String
BIOMETRIC_SERVICE = "biometric"
static final String
BLOB_STORE_SERVICE = "blob_store"
static final String
BLUETOOTH_SERVICE = "bluetooth"
static final String
BUGREPORT_SERVICE = "bugreport"
static final String
CAMERA_SERVICE = "camera"
static final String
CAPTIONING_SERVICE = "captioning"
static final String
CARRIER_CONFIG_SERVICE = "carrier_config"
static final String
CLIPBOARD_SERVICE = "clipboard"
static final String
COMPANION_DEVICE_SERVICE = "companiondevice"
static final String
CONNECTIVITY_DIAGNOSTICS_SERVICE = "connectivity_diagnostics"
static final String
CONNECTIVITY_SERVICE = "connectivity"
static final String
CONSUMER_IR_SERVICE = "consumer_ir"
static final int
static final int
static final int
static final String
CREDENTIAL_SERVICE = "credential"
static final String
CROSS_PROFILE_APPS_SERVICE = "crossprofileapps"
static final int
static final int
static final String
DEVICE_LOCK_SERVICE = "device_lock"
static final String
DEVICE_POLICY_SERVICE = "device_policy"
static final String
DISPLAY_HASH_SERVICE = "display_hash"
static final String
DISPLAY_SERVICE = "display"
static final String
DOMAIN_VERIFICATION_SERVICE = "domain_verification"
static final String
DOWNLOAD_SERVICE = "download"
static final String
DROPBOX_SERVICE = "dropbox"
static final String
EUICC_SERVICE = "euicc"
static final String
FILE_INTEGRITY_SERVICE = "file_integrity"
static final String
FINGERPRINT_SERVICE = "fingerprint"
static final String
GAME_SERVICE = "game"
static final String
GRAMMATICAL_INFLECTION_SERVICE = "grammatical_inflection"
static final String
HARDWARE_PROPERTIES_SERVICE = "hardware_properties"
static final String
HEALTHCONNECT_SERVICE = "healthconnect"
static final String
INPUT_METHOD_SERVICE = "input_method"
static final String
INPUT_SERVICE = "input"
static final String
IPSEC_SERVICE = "ipsec"
static final String
JOB_SCHEDULER_SERVICE = "jobscheduler"
static final String
KEYGUARD_SERVICE = "keyguard"
static final String
LAUNCHER_APPS_SERVICE = "launcherapps"
static final String
LAYOUT_INFLATER_SERVICE = "layout_inflater"
static final String
LOCALE_SERVICE = "locale"
static final String
LOCATION_SERVICE = "location"
static final String
MEDIA_COMMUNICATION_SERVICE = "media_communication"
static final String
MEDIA_METRICS_SERVICE = "media_metrics"
static final String
MEDIA_PROJECTION_SERVICE = "media_projection"
static final String
MEDIA_ROUTER_SERVICE = "media_router"
static final String
MEDIA_SESSION_SERVICE = "media_session"
static final String
MIDI_SERVICE = "midi"
static final int
MODE_APPEND = 32768
static final int
static final int

This field is deprecated.

static final int
static final int
static final int

This field is deprecated.

static final int

This field is deprecated.

static final String
static final String
NFC_SERVICE = "nfc"
static final String
NOTIFICATION_SERVICE = "notification"
static final String
NSD_SERVICE = "servicediscovery"
static final String
OVERLAY_SERVICE = "overlay"
static final String
PEOPLE_SERVICE = "people"
static final String
PERFORMANCE_HINT_SERVICE = "performance_hint"
static final String
POWER_SERVICE = "power"
static final String
PRINT_SERVICE = "print"
static final int
static final int
static final int
static final String
RESTRICTIONS_SERVICE = "restrictions"
static final String
ROLE_SERVICE = "role"
static final String
SEARCH_SERVICE = "search"
static final String
SENSOR_SERVICE = "sensor"
static final String
SHORTCUT_SERVICE = "shortcut"
static final String
STATUS_BAR_SERVICE = "statusbar"
static final String
STORAGE_SERVICE = "storage"
static final String
STORAGE_STATS_SERVICE = "storagestats"
static final String
SYSTEM_HEALTH_SERVICE = "systemhealth"
static final String
TELECOM_SERVICE = "telecom"
static final String
TELEPHONY_IMS_SERVICE = "telephony_ims"
static final String
static final String
TELEPHONY_SUBSCRIPTION_SERVICE = "telephony_subscription_service"
static final String
TEXT_CLASSIFICATION_SERVICE = "textclassification"
static final String
static final String
TV_INPUT_SERVICE = "tv_input"
static final String
TV_INTERACTIVE_APP_SERVICE = "tv_interactive_app"
static final String
UI_MODE_SERVICE = "uimode"
static final String
USAGE_STATS_SERVICE = "usagestats"
static final String
USB_SERVICE = "usb"
static final String
USER_SERVICE = "user"
static final String
VIBRATOR_MANAGER_SERVICE = "vibrator_manager"
static final String
VIBRATOR_SERVICE = "vibrator"

This field is deprecated.

static final String
VIRTUAL_DEVICE_SERVICE = "virtualdevice"
static final String
VPN_MANAGEMENT_SERVICE = "vpn_management"
static final String
WALLPAPER_SERVICE = "wallpaper"
static final String
WIFI_AWARE_SERVICE = "wifiaware"
static final String
WIFI_P2P_SERVICE = "wifip2p"
static final String
static final String
WIFI_SERVICE = "wifi"
static final String
WINDOW_SERVICE = "window"
From android.app.Service
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int

This field is deprecated.

static final int

Inherited methods

From android.content.Context
final int
getColor(int id)
final ColorStateList
final Drawable
getDrawable(int id)
final String
getString(int resId)
final T
<T> getSystemService(Class<T> serviceClass)
final CharSequence
getText(int resId)
final TypedArray
void
void
sendBroadcastWithMultiplePermissions(
    Intent intent,
    String[] receiverPermissions
)
From android.content.ContextWrapper
boolean
bindIsolatedService(
    Intent service,
    int flags,
    String instanceName,
    Executor executor,
    ServiceConnection conn
)
boolean
bindService(Intent service, ServiceConnection conn, int flags)
boolean
bindServiceAsUser(
    Intent service,
    ServiceConnection conn,
    int flags,
    UserHandle user
)
int
int
checkCallingOrSelfUriPermission(Uri uri, int modeFlags)
int[]
checkCallingOrSelfUriPermissions(List<Uri> uris, int modeFlags)
int
int
checkCallingUriPermission(Uri uri, int modeFlags)
int[]
checkCallingUriPermissions(List<Uri> uris, int modeFlags)
int
checkPermission(String permission, int pid, int uid)
int
int
checkUriPermission(Uri uri, int pid, int uid, int modeFlags)
int[]
checkUriPermissions(List<Uri> uris, int pid, int uid, int modeFlags)
void

This method is deprecated.

Context
Context
Context
Context
Context
createDeviceContext(int deviceId)
Context
Context
Context
createPackageContext(String packageName, int flags)
Context
createWindowContext(int type, Bundle options)
String[]
boolean
boolean
boolean
void
void
enforceCallingOrSelfUriPermission(
    Uri uri,
    int modeFlags,
    String message
)
void
enforceCallingPermission(String permission, String message)
void
enforceCallingUriPermission(Uri uri, int modeFlags, String message)
void
enforcePermission(String permission, int pid, int uid, String message)
void
enforceUriPermission(
    Uri uri,
    int pid,
    int uid,
    int modeFlags,
    String message
)
String[]
Context
ApplicationInfo
AssetManager
AttributionSource
String
Context
File
ClassLoader
File
ContentResolver
File
File
int
File
getDir(String name, int mode)
Display
File
File[]
File
File[]
File[]

This method is deprecated.

File
File
Executor
Looper
File
File
File[]
String
String
PackageManager
String
String
ContextParams
Resources
SharedPreferences
getSharedPreferences(String name, int mode)
Object
String
Resources.Theme
Drawable

This method is deprecated.

int

This method is deprecated.

int

This method is deprecated.

void
grantUriPermission(String toPackage, Uri uri, int modeFlags)
boolean
boolean
boolean
boolean
moveDatabaseFrom(Context sourceContext, String name)
boolean
moveSharedPreferencesFrom(Context sourceContext, String name)
FileInputStream
FileOutputStream
openFileOutput(String name, int mode)
SQLiteDatabase
openOrCreateDatabase(
    String name,
    int mode,
    SQLiteDatabase.CursorFactory factory
)
Drawable

This method is deprecated.

void
void
Intent
void

This method is deprecated.

void

This method is deprecated.

void
void
revokeUriPermission(Uri uri, int modeFlags)
void
void
void
sendOrderedBroadcast(Intent intent, String receiverPermission)
void
sendOrderedBroadcastAsUser(
    Intent intent,
    UserHandle user,
    String receiverPermission,
    BroadcastReceiver resultReceiver,
    Handler scheduler,
    int initialCode,
    String initialData,
    Bundle initialExtras
)
void

This method is deprecated.

void

This method is deprecated.

void
sendStickyOrderedBroadcast(
    Intent intent,
    BroadcastReceiver resultReceiver,
    Handler scheduler,
    int initialCode,
    String initialData,
    Bundle initialExtras
)

This method is deprecated.

void
sendStickyOrderedBroadcastAsUser(
    Intent intent,
    UserHandle user,
    BroadcastReceiver resultReceiver,
    Handler scheduler,
    int initialCode,
    String initialData,
    Bundle initialExtras
)

This method is deprecated.

void
setTheme(int resid)
void

This method is deprecated.

void
startActivities(Intent[] intents)
void
ComponentName
boolean
startInstrumentation(
    ComponentName className,
    String profileFile,
    Bundle arguments
)
void
startIntentSender(
    IntentSender intent,
    Intent fillInIntent,
    int flagsMask,
    int flagsValues,
    int extraFlags
)
ComponentName
boolean
void
void
void
void
void
updateServiceGroup(ServiceConnection conn, int group, int importance)
From android.app.Service
void
void
dump(FileDescriptor fd, PrintWriter writer, String[] args)
final Application
final int
void
void
void
onRebind(Intent intent)
void
onStart(Intent intent, int startId)

This method is deprecated.

void
onTimeout(int startId)
void
onTrimMemory(int level)
boolean
onUnbind(Intent intent)
final void
startForeground(int id, Notification notification)
final void
stopForeground(boolean removeNotification)

This method is deprecated.

final void
final boolean
stopSelfResult(int startId)

Constants

ACTION_ADD_DOWNLOAD

public static final String ACTION_ADD_DOWNLOAD = "androidx.media3.exoplayer.downloadService.action.ADD_DOWNLOAD"

Adds a new download. Extras:

ACTION_INIT

public static final String ACTION_INIT = "androidx.media3.exoplayer.downloadService.action.INIT"

Starts a download service to resume any ongoing downloads. Extras:

ACTION_PAUSE_DOWNLOADS

public static final String ACTION_PAUSE_DOWNLOADS = "androidx.media3.exoplayer.downloadService.action.PAUSE_DOWNLOADS"

Pauses all downloads. Extras:

ACTION_REMOVE_ALL_DOWNLOADS

public static final String ACTION_REMOVE_ALL_DOWNLOADS = "androidx.media3.exoplayer.downloadService.action.REMOVE_ALL_DOWNLOADS"

Removes all downloads. Extras:

ACTION_REMOVE_DOWNLOAD

public static final String ACTION_REMOVE_DOWNLOAD = "androidx.media3.exoplayer.downloadService.action.REMOVE_DOWNLOAD"

Removes a download. Extras:

ACTION_RESUME_DOWNLOADS

public static final String ACTION_RESUME_DOWNLOADS = "androidx.media3.exoplayer.downloadService.action.RESUME_DOWNLOADS"

Resumes all downloads except those that have a non-zero stopReason. Extras:

ACTION_SET_REQUIREMENTS

public static final String ACTION_SET_REQUIREMENTS = "androidx.media3.exoplayer.downloadService.action.SET_REQUIREMENTS"

Sets the requirements that need to be met for downloads to progress. Extras:

ACTION_SET_STOP_REASON

public static final String ACTION_SET_STOP_REASON = "androidx.media3.exoplayer.downloadService.action.SET_STOP_REASON"

Sets the stop reason for one or all downloads. To clear the stop reason, pass STOP_REASON_NONE. Extras:

DEFAULT_FOREGROUND_NOTIFICATION_UPDATE_INTERVAL

public static final long DEFAULT_FOREGROUND_NOTIFICATION_UPDATE_INTERVAL = 1000

Default foreground notification update interval in milliseconds.

FOREGROUND_NOTIFICATION_ID_NONE

public static final int FOREGROUND_NOTIFICATION_ID_NONE = 0

Invalid foreground notification id that can be used to run the service in the background.

KEY_CONTENT_ID

public static final String KEY_CONTENT_ID = "content_id"

Key for the String content id in ACTION_SET_STOP_REASON and ACTION_REMOVE_DOWNLOAD intents.

KEY_DOWNLOAD_REQUEST

public static final String KEY_DOWNLOAD_REQUEST = "download_request"

Key for the DownloadRequest in ACTION_ADD_DOWNLOAD intents.

KEY_FOREGROUND

public static final String KEY_FOREGROUND = "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 call startForeground.

KEY_REQUIREMENTS

public static final String KEY_REQUIREMENTS = "requirements"

Key for the Requirements in ACTION_SET_REQUIREMENTS intents.

KEY_STOP_REASON

public static final String KEY_STOP_REASON = "stop_reason"

Key for the integer stop reason in ACTION_SET_STOP_REASON and ACTION_ADD_DOWNLOAD intents.

Protected constructors

DownloadService

protected DownloadService(int foregroundNotificationId)

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
int 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
)

Creates a DownloadService.

Parameters
int foregroundNotificationId

The notification id for the foreground notification, or FOREGROUND_NOTIFICATION_ID_NONE if the service should only ever run in the background.

long 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
)

Creates a DownloadService.

Parameters
int foregroundNotificationId

The notification id for the foreground notification, or FOREGROUND_NOTIFICATION_ID_NONE if the service should only ever run in the background.

long foregroundNotificationUpdateInterval

The maximum interval between updates to the foreground notification, in milliseconds. Ignored if foregroundNotificationId is FOREGROUND_NOTIFICATION_ID_NONE.

@Nullable String 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.

@StringRes int 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.

@StringRes int 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.

Public methods

buildAddDownloadIntent

public static Intent buildAddDownloadIntent(
    Context context,
    Class<DownloadService> clazz,
    DownloadRequest downloadRequest,
    boolean foreground
)

Builds an Intent for adding a new download.

Parameters
Context context

A Context.

Class<DownloadService> clazz

The concrete download service being targeted by the intent.

DownloadRequest downloadRequest

The request to be executed.

boolean foreground

Whether this intent will be used to start the service in the foreground.

Returns
Intent

The created intent.

buildAddDownloadIntent

public static Intent buildAddDownloadIntent(
    Context context,
    Class<DownloadService> clazz,
    DownloadRequest downloadRequest,
    int stopReason,
    boolean foreground
)

Builds an Intent for adding a new download.

Parameters
Context context

A Context.

Class<DownloadService> clazz

The concrete download service being targeted by the intent.

DownloadRequest downloadRequest

The request to be executed.

int stopReason

An initial stop reason for the download, or STOP_REASON_NONE if the download should be started.

boolean foreground

Whether this intent will be used to start the service in the foreground.

Returns
Intent

The created intent.

buildPauseDownloadsIntent

public static Intent buildPauseDownloadsIntent(
    Context context,
    Class<DownloadService> clazz,
    boolean foreground
)

Builds an Intent to pause all downloads.

Parameters
Context context

A Context.

Class<DownloadService> clazz

The concrete download service being targeted by the intent.

boolean foreground

Whether this intent will be used to start the service in the foreground.

Returns
Intent

The created intent.

buildRemoveAllDownloadsIntent

public static Intent buildRemoveAllDownloadsIntent(
    Context context,
    Class<DownloadService> clazz,
    boolean foreground
)

Builds an Intent for removing all downloads.

Parameters
Context context

A Context.

Class<DownloadService> clazz

The concrete download service being targeted by the intent.

boolean foreground

Whether this intent will be used to start the service in the foreground.

Returns
Intent

The created intent.

buildRemoveDownloadIntent

public static Intent buildRemoveDownloadIntent(
    Context context,
    Class<DownloadService> clazz,
    String id,
    boolean foreground
)

Builds an Intent for removing the download with the id.

Parameters
Context context

A Context.

Class<DownloadService> clazz

The concrete download service being targeted by the intent.

String id

The content id.

boolean foreground

Whether this intent will be used to start the service in the foreground.

Returns
Intent

The created intent.

buildResumeDownloadsIntent

public static Intent buildResumeDownloadsIntent(
    Context context,
    Class<DownloadService> clazz,
    boolean foreground
)

Builds an Intent for resuming all downloads.

Parameters
Context context

A Context.

Class<DownloadService> clazz

The concrete download service being targeted by the intent.

boolean foreground

Whether this intent will be used to start the service in the foreground.

Returns
Intent

The created intent.

buildSetRequirementsIntent

public static Intent buildSetRequirementsIntent(
    Context context,
    Class<DownloadService> clazz,
    Requirements requirements,
    boolean foreground
)

Builds an Intent for setting the requirements that need to be met for downloads to progress.

Parameters
Context context

A Context.

Class<DownloadService> clazz

The concrete download service being targeted by the intent.

Requirements requirements

A Requirements.

boolean foreground

Whether this intent will be used to start the service in the foreground.

Returns
Intent

The created intent.

buildSetStopReasonIntent

public static Intent buildSetStopReasonIntent(
    Context context,
    Class<DownloadService> clazz,
    @Nullable String id,
    int stopReason,
    boolean foreground
)

Builds an Intent for setting the stop reason for one or all downloads. To clear the stop reason, pass STOP_REASON_NONE.

Parameters
Context context

A Context.

Class<DownloadService> clazz

The concrete download service being targeted by the intent.

@Nullable String id

The content id, or null to set the stop reason for all downloads.

int stopReason

An application defined stop reason.

boolean foreground

Whether this intent will be used to start the service in the foreground.

Returns
Intent

The created intent.

clearDownloadManagerHelpers

public static void clearDownloadManagerHelpers()

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.

onBind

public final @Nullable IBinder onBind(Intent intent)

Throws UnsupportedOperationException because this service is not designed to be bound.

onCreate

public void onCreate()

onDestroy

public void onDestroy()

onStartCommand

public int onStartCommand(@Nullable Intent intent, int flags, int startId)

onTaskRemoved

public void onTaskRemoved(Intent rootIntent)

sendAddDownload

public static void sendAddDownload(
    Context context,
    Class<DownloadService> clazz,
    DownloadRequest downloadRequest,
    boolean foreground
)

Starts the service if not started already and adds a new download.

Parameters
Context context

A Context.

Class<DownloadService> clazz

The concrete download service to be started.

DownloadRequest downloadRequest

The request to be executed.

boolean foreground

Whether the service is started in the foreground.

sendAddDownload

public static void sendAddDownload(
    Context context,
    Class<DownloadService> clazz,
    DownloadRequest downloadRequest,
    int stopReason,
    boolean foreground
)

Starts the service if not started already and adds a new download.

Parameters
Context context

A Context.

Class<DownloadService> clazz

The concrete download service to be started.

DownloadRequest downloadRequest

The request to be executed.

int stopReason

An initial stop reason for the download, or STOP_REASON_NONE if the download should be started.

boolean foreground

Whether the service is started in the foreground.

sendPauseDownloads

public static void sendPauseDownloads(
    Context context,
    Class<DownloadService> clazz,
    boolean foreground
)

Starts the service if not started already and pauses all downloads.

Parameters
Context context

A Context.

Class<DownloadService> clazz

The concrete download service to be started.

boolean foreground

Whether the service is started in the foreground.

sendRemoveAllDownloads

public static void sendRemoveAllDownloads(
    Context context,
    Class<DownloadService> clazz,
    boolean foreground
)

Starts the service if not started already and removes all downloads.

Parameters
Context context

A Context.

Class<DownloadService> clazz

The concrete download service to be started.

boolean foreground

Whether the service is started in the foreground.

sendRemoveDownload

public static void sendRemoveDownload(
    Context context,
    Class<DownloadService> clazz,
    String id,
    boolean foreground
)

Starts the service if not started already and removes a download.

Parameters
Context context

A Context.

Class<DownloadService> clazz

The concrete download service to be started.

String id

The content id.

boolean foreground

Whether the service is started in the foreground.

sendResumeDownloads

public static void sendResumeDownloads(
    Context context,
    Class<DownloadService> clazz,
    boolean foreground
)

Starts the service if not started already and resumes all downloads.

Parameters
Context context

A Context.

Class<DownloadService> clazz

The concrete download service to be started.

boolean foreground

Whether the service is started in the foreground.

sendSetRequirements

public static void sendSetRequirements(
    Context context,
    Class<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 context

A Context.

Class<DownloadService> clazz

The concrete download service to be started.

Requirements requirements

A Requirements.

boolean foreground

Whether the service is started in the foreground.

sendSetStopReason

public static void sendSetStopReason(
    Context context,
    Class<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, pass STOP_REASON_NONE.

Parameters
Context context

A Context.

Class<DownloadService> clazz

The concrete download service to be started.

@Nullable String id

The content id, or null to set the stop reason for all downloads.

int stopReason

An application defined stop reason.

boolean foreground

Whether the service is started in the foreground.

start

public static void start(Context context, Class<DownloadService> clazz)

Starts a download service to resume any ongoing downloads.

Parameters
Context context

A Context.

Class<DownloadService> clazz

The concrete download service to be started.

See also
startForeground

startForeground

public static void startForeground(Context context, Class<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 context

A Context.

Class<DownloadService> clazz

The concrete download service to be started.

See also
start

Protected methods

getDownloadManager

protected abstract DownloadManager getDownloadManager()

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.

getForegroundNotification

protected abstract Notification getForegroundNotification(
    List<Download> downloads,
    @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 to FOREGROUND_NOTIFICATION_ID_NONE. This method is not called for such services, meaning it can be implemented to throw UnsupportedOperationException.

Parameters
List<Download> downloads

The current downloads.

@Requirements.RequirementFlags int notMetRequirements

Any requirements for downloads that are not currently met.

Returns
Notification

The foreground notification to display.

getScheduler

protected abstract @Nullable Scheduler getScheduler()

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 Schedulersupports 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.

invalidateForegroundNotification

protected final void invalidateForegroundNotification()

Invalidates the current foreground notification and causes getForegroundNotification to be invoked again if the service isn't stopped.