public class DownloadSession extends Object
DownloadSession object and provides management
interfaces, such as starting, pausing, resuming, and deleting a session, and querying session details.
Note that applications must obtain the ohos.permission.INTERNET permission to use the interfaces.
SystemCapability.Miscservices#DOWNLOAD| 限定符和类型 | 类 | 说明 |
|---|---|---|
static class |
DownloadSession.DownloadInfo |
Provides methods for obtaining download session information and status.
|
| 限定符和类型 | 字段 | 说明 |
|---|---|---|
static int |
ERROR_CANNOT_RESUME |
Indicates that the download cannot be resumed for some temporary errors.
|
static int |
ERROR_DEVICE_NOT_FOUND |
Indicates that no storage device, such as an SD card, is found.
|
static int |
ERROR_FILE_ALREADY_EXISTS |
Indicates that files to be downloaded already exist, and that the download session cannot
overwrite the existing files.
|
static int |
ERROR_FILE_ERROR |
Indicates that a file operation fails.
|
static int |
ERROR_HTTP_DATA_ERROR |
Indicates that the HTTP transmission fails.
|
static int |
ERROR_INSUFFICIENT_SPACE |
Indicates insufficient storage space.
|
static int |
ERROR_TOO_MANY_REDIRECTS |
Indicates an error caused by too many network redirections.
|
static int |
ERROR_UNHANDLED_HTTP_CODE |
Indicates an HTTP code that cannot be identified.
|
static int |
ERROR_UNKNOWN |
Indicates an undefined error.
|
static int |
PAUSED_QUEUED_FOR_WIFI |
Indicates that the download is paused and waiting for a WLAN connection, because the file size
exceeds the maximum allowed for a session using the cellular network.
|
static int |
PAUSED_UNKNOWN |
Indicates that the download is paused for some reasons.
|
static int |
PAUSED_WAITING_FOR_NETWORK |
Indicates that the download is paused due to a network problem, for example, network disconnection.
|
static int |
PAUSED_WAITING_TO_RETRY |
Indicates that a network error occurs, and the download session will be retried.
|
static int |
SESSION_FAILED |
Indicates that the download session has failed and will not be retried.
|
static int |
SESSION_PAUSED |
Indicates that the download session has been paused.
|
static int |
SESSION_PENDING |
Indicates that the download session is being scheduled.
|
static int |
SESSION_RUNNING |
Indicates that the download session is in progress.
|
static int |
SESSION_SUCCESSFUL |
Indicates that the download session is completed.
|
| 构造器 | 说明 |
|---|---|
DownloadSession(Context context,
DownloadConfig config) |
A constructor used to create a
DownloadSession object based on a specified
DownloadConfig object. |
DownloadSession(Context context,
Uri uri) |
A constructor used to create a
DownloadSession object based on a specified HTTP URI. |
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
boolean |
addListener(IDownloadListener listener) |
Adds a download session listener, which is defined in
IDownloadListener. |
boolean |
attach(long id) |
Associates a newly created download session with a specified ID.
|
MessageParcel |
openDownloadedFile() |
Opens a downloaded file.
|
boolean |
pause() |
Pauses a download session.
|
DownloadSession.DownloadInfo |
query() |
Queries download information of a session, which is defined in
DownloadSession.DownloadInfo. |
String |
queryMimeType() |
Queries the MIME type of the download file.
|
boolean |
remove() |
Deletes a download session and the downloaded files.
|
boolean |
removeListener(IDownloadListener listener) |
Deletes a download session listener, which is defined in
IDownloadListener. |
boolean |
resume() |
Resumes a paused download session.
|
long |
start() |
Starts a download session.
|
public static final int ERROR_CANNOT_RESUME
public static final int ERROR_DEVICE_NOT_FOUND
public static final int ERROR_FILE_ALREADY_EXISTS
public static final int ERROR_FILE_ERROR
public static final int ERROR_HTTP_DATA_ERROR
public static final int ERROR_INSUFFICIENT_SPACE
public static final int ERROR_TOO_MANY_REDIRECTS
public static final int ERROR_UNHANDLED_HTTP_CODE
public static final int ERROR_UNKNOWN
public static final int PAUSED_QUEUED_FOR_WIFI
public static final int PAUSED_UNKNOWN
public static final int PAUSED_WAITING_FOR_NETWORK
public static final int PAUSED_WAITING_TO_RETRY
public static final int SESSION_FAILED
public static final int SESSION_PAUSED
public static final int SESSION_PENDING
public static final int SESSION_RUNNING
public static final int SESSION_SUCCESSFUL
public DownloadSession(Context context, DownloadConfig config)
DownloadSession object based on a specified
DownloadConfig object.context - Indicates the application context.config - Indicates a DownloadConfig object.attach(long)public DownloadSession(Context context, Uri uri)
DownloadSession object based on a specified HTTP URI.context - Indicates the application context.uri - Indicates an HTTP URI referenced to the file to download.NullPointerException - Throws this exception if the URI is null.IllegalArgumentException - Throws this exception if the URI is not in the HTTP or HTTPS format.public boolean attach(long id)
Applications should save download session IDs returned by start()
to local storage. After the application restarts,
create a download session and use this method to associate it with a stored session ID.
id - Indicates a download session ID.true if the session is associated with the ID; returns false otherwise.
The association may fail because the download session does not exist or it has been started.public long start()
If the session is successfully started, the session ID is returned. Keep the ID properly. It will be used for the download session after the application restarts.
The ohos.permission.INTERNET permission is required to use this method.
-1 otherwise.attach(long)public boolean remove()
true if the session is deleted; returns false otherwise.public boolean pause()
true if the download is paused; returns false otherwise.resume()public boolean resume()
Only if the server providing files supports resumable upload, the download session can be resumed.
true if the download is resumed; returns false otherwise.pause()public DownloadSession.DownloadInfo query()
DownloadSession.DownloadInfo.null otherwise.DownloadSession.DownloadInfopublic boolean addListener(IDownloadListener listener)
IDownloadListener.
The ohos.permission.INTERNET permission is required to use this method.
listener - Indicates the listener to be added.true if the listener is added, returns false otherwise.public boolean removeListener(IDownloadListener listener)
IDownloadListener.
The ohos.permission.INTERNET permission is required to use this method.
listener - Indicates the listener to be deleted.true if the listener is deleted, returns false otherwise.public MessageParcel openDownloadedFile() throws FileNotFoundException
MessageParcel.FileNotFoundException - Throws this exception if the file cannot be opened.public String queryMimeType()
null otherwise.