Class BackgroundTransfer

java.lang.Object
com.codename1.io.BackgroundTransfer

public final class BackgroundTransfer extends Object

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);
    }
});
  • 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: 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: 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: the FileSystemStorage path 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

      public static BackgroundTransfer.Transfer[] getActiveTransfers()