Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.
public final class BackgroundTransfer
- Object
- 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 types
interface BackgroundTransfer.Listener | Receives transfer progress and completion on the EDT. |
class BackgroundTransfer.Transfer | A transfer started by #download or #upload. |
Methods
Inherited methods
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
urlString- the URL to fetch
destinationPathString- the
FileSystemStoragepath 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 listenerBackgroundTransfer.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 IOExceptionUploads a file as a multipart form POST.
Parameters
urlString- the URL to post to
filePathString- the
FileSystemStoragepath of the file fieldNameString- the form field name
mimeTypeString- the file MIME type
listenerBackgroundTransfer.Listener- progress and completion listener; may be null
Returns
the transfer
Throws
IOException- if the file cannot be read
getActiveTransfers
public static BackgroundTransfer.Transfer[] getActiveTransfers()