Class BackgroundTransfer
File downloads and uploads with progress and a completion listener.
Transfers run on the network thread and report on the EDT. They continue
while the app is in the background for as long as the operating system
keeps the process alive. #isProcessIndependent() tells whether the
platform hands transfers to a system service that also survives the
process; it answers false on every platform today, so a transfer that
matters should be restarted when the app launches and finds it
incomplete.
String dest = FileSystemStorage.getInstance().getAppHomePath() + "video.mp4";
BackgroundTransfer.download("https://example.com/video.mp4", dest,
new BackgroundTransfer.Listener() {
public void onProgress(BackgroundTransfer.Transfer t, long done, long total) {
Log.p("Downloaded " + done + " of " + total);
}
public void onComplete(BackgroundTransfer.Transfer t, boolean success, int status) {
Log.p("Download finished: " + success);
}
});
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceReceives transfer progress and completion on the EDT.static final classA transfer started by#downloador#upload. -
Method Summary
Modifier and TypeMethodDescriptionstatic BackgroundTransfer.Transferdownload(String url, String destinationPath, BackgroundTransfer.Listener listener) Downloads a URL into a file.static BackgroundTransfer.Transfer[]static booleanTrue when transfers are handed to a system service that survives the app process.static BackgroundTransfer.Transferupload(String url, String filePath, String fieldName, String mimeType, BackgroundTransfer.Listener listener) Uploads a file as a multipart form POST.
-
Method Details
-
isProcessIndependent
public static boolean isProcessIndependent()True when transfers are handed to a system service that survives the app process.
Returns
false on every platform today
-
download
public static BackgroundTransfer.Transfer download(String url, String destinationPath, BackgroundTransfer.Listener listener) Downloads a URL into a file.
Parameters
-
url: the URL to fetch -
destinationPath: theFileSystemStoragepath to write. The data goes to the same path plus.partfirst and replaces the target only when the response succeeded; a failed or cancelled download leaves the target as it was -
listener: progress and completion listener; may be null
Returns
the transfer
-
-
upload
public static BackgroundTransfer.Transfer upload(String url, String filePath, String fieldName, String mimeType, BackgroundTransfer.Listener listener) throws IOException Uploads a file as a multipart form POST.
Parameters
-
url: the URL to post to -
filePath: theFileSystemStoragepath of the file -
fieldName: the form field name -
mimeType: the file MIME type -
listener: progress and completion listener; may be null
Returns
the transfer
Throws
IOException: if the file cannot be read
- Throws:
IOException
-
-
getActiveTransfers
-