Skip to content

Monitoring upload status

Alex Gotev edited this page Jun 22, 2016 · 41 revisions

To listen for the status of the upload tasks, you have the following choices:

  • Starting from release 3.0, you can handle upload status events using also callbacks. To use callbacks, invoke the setDelegate method on the UploadRequest, like this:

    public void uploadMultipart(final Context context) {
        try {
            String uploadId =
              new MultipartUploadRequest(context, "http://upload.server.com/path")
                .addFileToUpload("/absolute/path/to/your/file", "your-param-name")
                .setNotificationConfig(new UploadNotificationConfig())
                .setMaxRetries(2)
                .setDelegate(new UploadStatusDelegate() {
                     @Override
                     public void onProgress(UploadInfo uploadInfo) {
                         // your code here
                     }
    
                     @Override
                     public void onError(UploadInfo uploadInfo, Exception exception) {
                        // your code here
                     }
    
                     @Override
                     public void onCompleted(UploadInfo uploadInfo, ServerResponse serverResponse) {
                        // your code here
                     }
    
                     @Override
                     public void onCancelled(UploadInfo uploadInfo) {
                        // your code here
                     }
                })
                .startUpload();
        } catch (Exception exc) {
            Log.e("AndroidUploadService", exc.getMessage(), exc);
        }
    }

    When you set the delegate for an UploadRequest, all the events will be pushed to it and will not be broadcasted. So, if you want to handle events using UploadServiceBroadcastReceiver, just do not invoke the setDelegate method. UploadServiceBroadcastReceiver gives you more flexibility (you can generate upload requests in one point of your code and react to events in another service or activity for example), while the delegate is simpler if all you need to do is handle all the events inside the same activity or service which originated the request.

  • Create a new subclass of UploadServiceBroadcastReceiver inside a Service or Activity (check register and unregister methods JavaDoc for detailed instructions)

private UploadServiceBroadcastReceiver broadcastReceiver = new UploadServiceBroadcastReceiver() { @Override public void onProgress(UploadInfo uploadInfo) { // your code here }

    @Override
    public void onCancelled(UploadInfo uploadInfo) {
        // your code here
    }

    @Override
    public void onError(UploadInfo uploadInfo, Exception exception) {
        // your code here
    }

    @Override
    public void onCompleted(UploadInfo uploadInfo, ServerResponse serverResponse) {
        // your code here
    }
};
```
  • create a new class (e.g. MyReceiver) which extends UploadServiceBroadcastReceiver, add your business logic in it and then register it as a broadcast receiver in your manifest (as shown below), with the intent filter com.yourcompany.yourapp.uploadservice.broadcast.status. This way you can listen for events independently from your activities and services. Change com.yourcompany.yourapp with whatever you have set as UploadService.NAMESPACE in the initial setup:
    <receiver android:name="MyReceiver">
      <intent-filter>
          <action android:name="com.yourcompany.yourapp.uploadservice.broadcast.status" />
      </intent-filter>
    </receiver>

MyReceiver.java ```java public class MyReceiver extends UploadServiceBroadcastReceiver { @Override public void onProgress(UploadInfo uploadInfo) { // your code here }

    @Override
    public void onCancelled(UploadInfo uploadInfo) {
        // your code here
    }

    @Override
    public void onError(UploadInfo uploadInfo, Exception exception) {
        // your code here
    }

    @Override
    public void onCompleted(UploadInfo uploadInfo, ServerResponse serverResponse) {
        // your code here
    }
}
```
Clone this wiki locally