Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.

public final class BackgroundTransfer

  1. Object
  2. 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.ListenerReceives transfer progress and completion on the EDT.
class BackgroundTransfer.TransferA transfer started by #download or #upload.

Methods

public static boolean isProcessIndependent()True when transfers are handed to a system service that survives the app process.
public static BackgroundTransfer.Transfer download(String url, String destinationPath, BackgroundTransfer.Listener listener)Downloads a URL into a file.
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.
public static BackgroundTransfer.Transfer[] getActiveTransfers()

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

url String
the URL to fetch
destinationPath String
the FileSystemStorage path to write. The data goes to the same path plus .part first and replaces the target only when the response succeeded; a failed or cancelled download leaves the target as it was
listener BackgroundTransfer.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 String
the URL to post to
filePath String
the FileSystemStorage path of the file
fieldName String
the form field name
mimeType String
the file MIME type
listener BackgroundTransfer.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()