DownloadService


@UnstableApi
abstract class DownloadService : 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

const String!
ACTION_ADD_DOWNLOAD = "androidx.media3.exoplayer.downloadService.action.ADD_DOWNLOAD"

Adds a new download.

const String!
ACTION_INIT = "androidx.media3.exoplayer.downloadService.action.INIT"

Starts a download service to resume any ongoing downloads.

const String!
ACTION_PAUSE_DOWNLOADS = "androidx.media3.exoplayer.downloadService.action.PAUSE_DOWNLOADS"

Pauses all downloads.

const String!
ACTION_REMOVE_ALL_DOWNLOADS = "androidx.media3.exoplayer.downloadService.action.REMOVE_ALL_DOWNLOADS"

Removes all downloads.

const String!
ACTION_REMOVE_DOWNLOAD = "androidx.media3.exoplayer.downloadService.action.REMOVE_DOWNLOAD"

Removes a download.

const String!
ACTION_RESUME_DOWNLOADS = "androidx.media3.exoplayer.downloadService.action.RESUME_DOWNLOADS"

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

const String!
ACTION_SET_REQUIREMENTS = "androidx.media3.exoplayer.downloadService.action.SET_REQUIREMENTS"

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

const String!
ACTION_SET_STOP_REASON = "androidx.media3.exoplayer.downloadService.action.SET_STOP_REASON"

Sets the stop reason for one or all downloads.

const Long

Default foreground notification update interval in milliseconds.

const Int

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

const String!
KEY_CONTENT_ID = "content_id"

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

const String!
KEY_DOWNLOAD_REQUEST = "download_request"

Key for the DownloadRequest in ACTION_ADD_DOWNLOAD intents.

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

const String!
KEY_REQUIREMENTS = "requirements"

Key for the Requirements in ACTION_SET_REQUIREMENTS intents.

const 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(foregroundNotificationId: Int)

Creates a DownloadService.

DownloadService(
    foregroundNotificationId: Int,
    foregroundNotificationUpdateInterval: Long
)

Creates a DownloadService.

DownloadService(
    foregroundNotificationId: Int,
    foregroundNotificationUpdateInterval: Long,
    channelId: String?,
    channelNameResourceId: @StringRes Int,
    channelDescriptionResourceId: @StringRes Int
)

Creates a DownloadService.

Public functions

java-static Intent!
buildAddDownloadIntent(
    context: Context!,
    clazz: Class<DownloadService!>!,
    downloadRequest: DownloadRequest!,
    foreground: Boolean
)

Builds an Intent for adding a new download.

java-static Intent!
buildAddDownloadIntent(
    context: Context!,
    clazz: Class<DownloadService!>!,
    downloadRequest: DownloadRequest!,
    stopReason: Int,
    foreground: Boolean
)

Builds an Intent for adding a new download.

java-static Intent!
buildPauseDownloadsIntent(
    context: Context!,
    clazz: Class<DownloadService!>!,
    foreground: Boolean
)

Builds an Intent to pause all downloads.

java-static Intent!
buildRemoveAllDownloadsIntent(
    context: Context!,
    clazz: Class<DownloadService!>!,
    foreground: Boolean
)

Builds an Intent for removing all downloads.

java-static Intent!
buildRemoveDownloadIntent(
    context: Context!,
    clazz: Class<DownloadService!>!,
    id: String!,
    foreground: Boolean
)

Builds an Intent for removing the download with the id.

java-static Intent!
buildResumeDownloadsIntent(
    context: Context!,
    clazz: Class<DownloadService!>!,
    foreground: Boolean
)

Builds an Intent for resuming all downloads.

java-static Intent!
buildSetRequirementsIntent(
    context: Context!,
    clazz: Class<DownloadService!>!,
    requirements: Requirements!,
    foreground: Boolean
)

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

java-static Intent!
buildSetStopReasonIntent(
    context: Context!,
    clazz: Class<DownloadService!>!,
    id: String?,
    stopReason: Int,
    foreground: Boolean
)

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

java-static Unit

Clear all download manager helpers before restarting the service.

IBinder?
onBind(intent: Intent!)

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

Unit
Unit
Int
onStartCommand(intent: Intent?, flags: Int, startId: Int)
Unit
onTaskRemoved(rootIntent: Intent!)
java-static Unit
sendAddDownload(
    context: Context!,
    clazz: Class<DownloadService!>!,
    downloadRequest: DownloadRequest!,
    foreground: Boolean
)

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

java-static Unit
sendAddDownload(
    context: Context!,
    clazz: Class<DownloadService!>!,
    downloadRequest: DownloadRequest!,
    stopReason: Int,
    foreground: Boolean
)

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

java-static Unit
sendPauseDownloads(
    context: Context!,
    clazz: Class<DownloadService!>!,
    foreground: Boolean
)

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

java-static Unit
sendRemoveAllDownloads(
    context: Context!,
    clazz: Class<DownloadService!>!,
    foreground: Boolean
)

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

java-static Unit
sendRemoveDownload(
    context: Context!,
    clazz: Class<DownloadService!>!,
    id: String!,
    foreground: Boolean
)

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

java-static Unit
sendResumeDownloads(
    context: Context!,
    clazz: Class<DownloadService!>!,
    foreground: Boolean
)

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

java-static Unit
sendSetRequirements(
    context: Context!,
    clazz: Class<DownloadService!>!,
    requirements: Requirements!,
    foreground: Boolean
)

Starts the service if not started already and sets the requirements that need to be met for downloads to progress.

java-static Unit
sendSetStopReason(
    context: Context!,
    clazz: Class<DownloadService!>!,
    id: String?,
    stopReason: Int,
    foreground: Boolean
)

Starts the service if not started already and sets the stop reason for one or all downloads.

java-static Unit
start(context: Context!, clazz: Class<DownloadService!>!)

Starts a download service to resume any ongoing downloads.

java-static Unit
startForeground(context: Context!, clazz: Class<DownloadService!>!)

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

Protected functions

abstract DownloadManager!

Returns a DownloadManager to be used to downloaded content.

abstract Notification!
getForegroundNotification(
    downloads: (Mutable)List<Download!>!,
    @Requirements.RequirementFlags notMetRequirements: Int
)

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

abstract Scheduler?

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

Unit

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

Inherited Constants

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

This property is deprecated.

const Int
const Int
const Int

This property is deprecated.

const Int

This property is deprecated.

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

This property is deprecated.

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

Inherited functions

From android.content.Context
From android.content.ContextWrapper
Boolean
bindIsolatedService(
    service: Intent!,
    flags: Int,
    instanceName: String!,
    executor: Executor!,
    conn: ServiceConnection!
)
Boolean
bindService(service: Intent!, conn: ServiceConnection!, flags: Int)
Boolean
bindServiceAsUser(
    service: Intent!,
    conn: ServiceConnection!,
    flags: Int,
    user: UserHandle!
)
Int
Int
IntArray<Int>!
checkCallingOrSelfUriPermissions(
    uris: (Mutable)List<Uri!>!,
    modeFlags: Int
)
Int
Int
checkCallingUriPermission(uri: Uri!, modeFlags: Int)
IntArray<Int>!
checkCallingUriPermissions(uris: (Mutable)List<Uri!>!, modeFlags: Int)
Int
checkPermission(permission: String!, pid: Int, uid: Int)
Int
Int
checkUriPermission(uri: Uri!, pid: Int, uid: Int, modeFlags: Int)
IntArray<Int>!
checkUriPermissions(
    uris: (Mutable)List<Uri!>!,
    pid: Int,
    uid: Int,
    modeFlags: Int
)
Unit

This function is deprecated.

Context!
createAttributionContext(attributionTag: String!)
Context!
createConfigurationContext(overrideConfiguration: Configuration!)
Context!
createContext(contextParams: ContextParams!)
Context!
Context!
Context!
Context!
Context!
createPackageContext(packageName: String!, flags: Int)
Context!
createWindowContext(type: Int, options: Bundle!)
Array<String!>!
Boolean
Boolean
Boolean
Unit
enforceCallingOrSelfPermission(permission: String!, message: String!)
Unit
enforceCallingOrSelfUriPermission(
    uri: Uri!,
    modeFlags: Int,
    message: String!
)
Unit
enforceCallingPermission(permission: String!, message: String!)
Unit
enforceCallingUriPermission(uri: Uri!, modeFlags: Int, message: String!)
Unit
enforcePermission(permission: String!, pid: Int, uid: Int, message: String!)
Unit
enforceUriPermission(
    uri: Uri!,
    pid: Int,
    uid: Int,
    modeFlags: Int,
    message: String!
)
Array<String!>!
Context!
ApplicationInfo!
AssetManager!
AttributionSource!
String!
Context!
File!
ClassLoader!
File!
ContentResolver!
File!
File!
Int
File!
getDir(name: String!, mode: Int)
Display!
File!
Array<File!>!
File!
Array<File!>!
Array<File!>!

This function is deprecated.

File!
File!
Executor!
Looper!
File!
File!
Array<File!>!
String!
String!
PackageManager!
String!
String!
ContextParams!
Resources!
SharedPreferences!
getSharedPreferences(name: String!, mode: Int)
Any!
String!
getSystemServiceName(serviceClass: Class<Any!>!)
Resources.Theme!
Drawable!

This function is deprecated.

Int

This function is deprecated.

Int

This function is deprecated.

Unit
grantUriPermission(toPackage: String!, uri: Uri!, modeFlags: Int)
Boolean
Boolean
Boolean
Boolean
moveDatabaseFrom(sourceContext: Context!, name: String!)
Boolean
moveSharedPreferencesFrom(sourceContext: Context!, name: String!)
FileInputStream!
FileOutputStream!
openFileOutput(name: String!, mode: Int)
SQLiteDatabase!
openOrCreateDatabase(
    name: String!,
    mode: Int,
    factory: SQLiteDatabase.CursorFactory!
)
Drawable!

This function is deprecated.

Unit
Unit
Intent!
Unit

This function is deprecated.

Unit

This function is deprecated.

Unit
Unit
revokeUriPermission(uri: Uri!, modeFlags: Int)
Unit
Unit
Unit
sendOrderedBroadcast(intent: Intent!, receiverPermission: String!)
Unit
sendOrderedBroadcastAsUser(
    intent: Intent!,
    user: UserHandle!,
    receiverPermission: String!,
    resultReceiver: BroadcastReceiver!,
    scheduler: Handler!,
    initialCode: Int,
    initialData: String!,
    initialExtras: Bundle!
)
Unit

This function is deprecated.

Unit

This function is deprecated.

Unit
sendStickyOrderedBroadcast(
    intent: Intent!,
    resultReceiver: BroadcastReceiver!,
    scheduler: Handler!,
    initialCode: Int,
    initialData: String!,
    initialExtras: Bundle!
)

This function is deprecated.

Unit
sendStickyOrderedBroadcastAsUser(
    intent: Intent!,
    user: UserHandle!,
    resultReceiver: BroadcastReceiver!,
    scheduler: Handler!,
    initialCode: Int,
    initialData: String!,
    initialExtras: Bundle!
)

This function is deprecated.

Unit
setTheme(resid: Int)
Unit
setWallpaper(bitmap: Bitmap!)

This function is deprecated.

Unit
Unit
ComponentName!
Boolean
startInstrumentation(
    className: ComponentName!,
    profileFile: String!,
    arguments: Bundle!
)
Unit
startIntentSender(
    intent: IntentSender!,
    fillInIntent: Intent!,
    flagsMask: Int,
    flagsValues: Int,
    extraFlags: Int
)
ComponentName!
startService(service: Intent!)
Boolean
Unit
Unit
Unit
Unit
Unit
updateServiceGroup(conn: ServiceConnection!, group: Int, importance: Int)
From android.app.Service
Unit
Unit
dump(fd: FileDescriptor!, writer: PrintWriter!, args: Array<String!>!)
Application!
Int
Unit
Unit
Unit
onRebind(intent: Intent!)
Unit
onStart(intent: Intent!, startId: Int)

This function is deprecated.

Unit
onTimeout(startId: Int)
Unit
onTrimMemory(level: Int)
Boolean
onUnbind(intent: Intent!)
Unit
startForeground(id: Int, notification: Notification!)
Unit
stopForeground(removeNotification: Boolean)

This function is deprecated.

Unit
Boolean
stopSelfResult(startId: Int)

Constants

ACTION_ADD_DOWNLOAD

const val ACTION_ADD_DOWNLOAD = "androidx.media3.exoplayer.downloadService.action.ADD_DOWNLOAD": String!

Adds a new download. Extras:

ACTION_INIT

const val ACTION_INIT = "androidx.media3.exoplayer.downloadService.action.INIT": String!

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

ACTION_PAUSE_DOWNLOADS

const val ACTION_PAUSE_DOWNLOADS = "androidx.media3.exoplayer.downloadService.action.PAUSE_DOWNLOADS": String!

Pauses all downloads. Extras:

ACTION_REMOVE_ALL_DOWNLOADS

const val ACTION_REMOVE_ALL_DOWNLOADS = "androidx.media3.exoplayer.downloadService.action.REMOVE_ALL_DOWNLOADS": String!

Removes all downloads. Extras:

ACTION_REMOVE_DOWNLOAD

const val ACTION_REMOVE_DOWNLOAD = "androidx.media3.exoplayer.downloadService.action.REMOVE_DOWNLOAD": String!

Removes a download. Extras:

ACTION_RESUME_DOWNLOADS

const val ACTION_RESUME_DOWNLOADS = "androidx.media3.exoplayer.downloadService.action.RESUME_DOWNLOADS": String!

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

ACTION_SET_REQUIREMENTS

const val ACTION_SET_REQUIREMENTS = "androidx.media3.exoplayer.downloadService.action.SET_REQUIREMENTS": String!

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

ACTION_SET_STOP_REASON

const val ACTION_SET_STOP_REASON = "androidx.media3.exoplayer.downloadService.action.SET_STOP_REASON": String!

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

DEFAULT_FOREGROUND_NOTIFICATION_UPDATE_INTERVAL

const val DEFAULT_FOREGROUND_NOTIFICATION_UPDATE_INTERVAL = 1000: Long

Default foreground notification update interval in milliseconds.

FOREGROUND_NOTIFICATION_ID_NONE

const val FOREGROUND_NOTIFICATION_ID_NONE = 0: Int

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

KEY_CONTENT_ID

const val KEY_CONTENT_ID = "content_id": String!

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

KEY_DOWNLOAD_REQUEST

const val KEY_DOWNLOAD_REQUEST = "download_request": String!

Key for the DownloadRequest in ACTION_ADD_DOWNLOAD intents.

KEY_FOREGROUND

const val KEY_FOREGROUND = "foreground": String!

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

const val KEY_REQUIREMENTS = "requirements": String!

Key for the Requirements in ACTION_SET_REQUIREMENTS intents.

KEY_STOP_REASON

const val KEY_STOP_REASON = "stop_reason": String!

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

Protected constructors

DownloadService

protected DownloadService(foregroundNotificationId: Int)

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: Int

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(
    foregroundNotificationId: Int,
    foregroundNotificationUpdateInterval: Long
)

Creates a DownloadService.

Parameters
foregroundNotificationId: Int

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

foregroundNotificationUpdateInterval: Long

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

DownloadService

protected DownloadService(
    foregroundNotificationId: Int,
    foregroundNotificationUpdateInterval: Long,
    channelId: String?,
    channelNameResourceId: @StringRes Int,
    channelDescriptionResourceId: @StringRes Int
)

Creates a DownloadService.

Parameters
foregroundNotificationId: Int

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

foregroundNotificationUpdateInterval: Long

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

channelId: String?

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: @StringRes Int

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: @StringRes Int

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 functions

buildAddDownloadIntent

java-static fun buildAddDownloadIntent(
    context: Context!,
    clazz: Class<DownloadService!>!,
    downloadRequest: DownloadRequest!,
    foreground: Boolean
): Intent!

Builds an Intent for adding a new download.

Parameters
context: Context!

A Context.

clazz: Class<DownloadService!>!

The concrete download service being targeted by the intent.

downloadRequest: DownloadRequest!

The request to be executed.

foreground: Boolean

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

Returns
Intent!

The created intent.

buildAddDownloadIntent

java-static fun buildAddDownloadIntent(
    context: Context!,
    clazz: Class<DownloadService!>!,
    downloadRequest: DownloadRequest!,
    stopReason: Int,
    foreground: Boolean
): Intent!

Builds an Intent for adding a new download.

Parameters
context: Context!

A Context.

clazz: Class<DownloadService!>!

The concrete download service being targeted by the intent.

downloadRequest: DownloadRequest!

The request to be executed.

stopReason: Int

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

foreground: Boolean

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

Returns
Intent!

The created intent.

buildPauseDownloadsIntent

java-static fun buildPauseDownloadsIntent(
    context: Context!,
    clazz: Class<DownloadService!>!,
    foreground: Boolean
): Intent!

Builds an Intent to pause all downloads.

Parameters
context: Context!

A Context.

clazz: Class<DownloadService!>!

The concrete download service being targeted by the intent.

foreground: Boolean

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

Returns
Intent!

The created intent.

buildRemoveAllDownloadsIntent

java-static fun buildRemoveAllDownloadsIntent(
    context: Context!,
    clazz: Class<DownloadService!>!,
    foreground: Boolean
): Intent!

Builds an Intent for removing all downloads.

Parameters
context: Context!

A Context.

clazz: Class<DownloadService!>!

The concrete download service being targeted by the intent.

foreground: Boolean

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

Returns
Intent!

The created intent.

buildRemoveDownloadIntent

java-static fun buildRemoveDownloadIntent(
    context: Context!,
    clazz: Class<DownloadService!>!,
    id: String!,
    foreground: Boolean
): Intent!

Builds an Intent for removing the download with the id.

Parameters
context: Context!

A Context.

clazz: Class<DownloadService!>!

The concrete download service being targeted by the intent.

id: String!

The content id.

foreground: Boolean

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

Returns
Intent!

The created intent.

buildResumeDownloadsIntent

java-static fun buildResumeDownloadsIntent(
    context: Context!,
    clazz: Class<DownloadService!>!,
    foreground: Boolean
): Intent!

Builds an Intent for resuming all downloads.

Parameters
context: Context!

A Context.

clazz: Class<DownloadService!>!

The concrete download service being targeted by the intent.

foreground: Boolean

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

Returns
Intent!

The created intent.

buildSetRequirementsIntent

java-static fun buildSetRequirementsIntent(
    context: Context!,
    clazz: Class<DownloadService!>!,
    requirements: Requirements!,
    foreground: Boolean
): Intent!

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

Parameters
context: Context!

A Context.

clazz: Class<DownloadService!>!

The concrete download service being targeted by the intent.

requirements: Requirements!

A Requirements.

foreground: Boolean

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

Returns
Intent!

The created intent.

buildSetStopReasonIntent

java-static fun buildSetStopReasonIntent(
    context: Context!,
    clazz: Class<DownloadService!>!,
    id: String?,
    stopReason: Int,
    foreground: Boolean
): Intent!

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.

clazz: Class<DownloadService!>!

The concrete download service being targeted by the intent.

id: String?

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

stopReason: Int

An application defined stop reason.

foreground: Boolean

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

Returns
Intent!

The created intent.

clearDownloadManagerHelpers

java-static fun clearDownloadManagerHelpers(): Unit

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

fun onBind(intent: Intent!): IBinder?

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

onCreate

fun onCreate(): Unit

onDestroy

fun onDestroy(): Unit

onStartCommand

fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int

onTaskRemoved

fun onTaskRemoved(rootIntent: Intent!): Unit

sendAddDownload

java-static fun sendAddDownload(
    context: Context!,
    clazz: Class<DownloadService!>!,
    downloadRequest: DownloadRequest!,
    foreground: Boolean
): Unit

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

Parameters
context: Context!

A Context.

clazz: Class<DownloadService!>!

The concrete download service to be started.

downloadRequest: DownloadRequest!

The request to be executed.

foreground: Boolean

Whether the service is started in the foreground.

sendAddDownload

java-static fun sendAddDownload(
    context: Context!,
    clazz: Class<DownloadService!>!,
    downloadRequest: DownloadRequest!,
    stopReason: Int,
    foreground: Boolean
): Unit

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

Parameters
context: Context!

A Context.

clazz: Class<DownloadService!>!

The concrete download service to be started.

downloadRequest: DownloadRequest!

The request to be executed.

stopReason: Int

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

foreground: Boolean

Whether the service is started in the foreground.

sendPauseDownloads

java-static fun sendPauseDownloads(
    context: Context!,
    clazz: Class<DownloadService!>!,
    foreground: Boolean
): Unit

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

Parameters
context: Context!

A Context.

clazz: Class<DownloadService!>!

The concrete download service to be started.

foreground: Boolean

Whether the service is started in the foreground.

sendRemoveAllDownloads

java-static fun sendRemoveAllDownloads(
    context: Context!,
    clazz: Class<DownloadService!>!,
    foreground: Boolean
): Unit

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

Parameters
context: Context!

A Context.

clazz: Class<DownloadService!>!

The concrete download service to be started.

foreground: Boolean

Whether the service is started in the foreground.

sendRemoveDownload

java-static fun sendRemoveDownload(
    context: Context!,
    clazz: Class<DownloadService!>!,
    id: String!,
    foreground: Boolean
): Unit

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

Parameters
context: Context!

A Context.

clazz: Class<DownloadService!>!

The concrete download service to be started.

id: String!

The content id.

foreground: Boolean

Whether the service is started in the foreground.

sendResumeDownloads

java-static fun sendResumeDownloads(
    context: Context!,
    clazz: Class<DownloadService!>!,
    foreground: Boolean
): Unit

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

Parameters
context: Context!

A Context.

clazz: Class<DownloadService!>!

The concrete download service to be started.

foreground: Boolean

Whether the service is started in the foreground.

sendSetRequirements

java-static fun sendSetRequirements(
    context: Context!,
    clazz: Class<DownloadService!>!,
    requirements: Requirements!,
    foreground: Boolean
): Unit

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.

clazz: Class<DownloadService!>!

The concrete download service to be started.

requirements: Requirements!

A Requirements.

foreground: Boolean

Whether the service is started in the foreground.

sendSetStopReason

java-static fun sendSetStopReason(
    context: Context!,
    clazz: Class<DownloadService!>!,
    id: String?,
    stopReason: Int,
    foreground: Boolean
): Unit

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.

clazz: Class<DownloadService!>!

The concrete download service to be started.

id: String?

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

stopReason: Int

An application defined stop reason.

foreground: Boolean

Whether the service is started in the foreground.

start

java-static fun start(context: Context!, clazz: Class<DownloadService!>!): Unit

Starts a download service to resume any ongoing downloads.

Parameters
context: Context!

A Context.

clazz: Class<DownloadService!>!

The concrete download service to be started.

See also
startForeground

startForeground

java-static fun startForeground(context: Context!, clazz: Class<DownloadService!>!): Unit

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.

clazz: Class<DownloadService!>!

The concrete download service to be started.

See also
start

Protected functions

getDownloadManager

protected abstract fun getDownloadManager(): DownloadManager!

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 fun getForegroundNotification(
    downloads: (Mutable)List<Download!>!,
    @Requirements.RequirementFlags notMetRequirements: Int
): Notification!

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: (Mutable)List<Download!>!

The current downloads.

@Requirements.RequirementFlags notMetRequirements: Int

Any requirements for downloads that are not currently met.

Returns
Notification!

The foreground notification to display.

getScheduler

protected abstract fun getScheduler(): Scheduler?

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 fun invalidateForegroundNotification(): Unit

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