Skip to content

Monitoring upload status

Alex Gotev edited this page Aug 20, 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. If you set the delegate inside an activity, pay attention to its lifecycle, e.g. if you create a request with a delegate inside an activity and then you close it, the request will run in the background, but no callback methods will be invoked, because the delegate has been created inside the activity, which no longer exists. Delegates works best when used inside a service or an actvity from which the user never goes away during the entire upload process. If this is not your case, I suggest you to use one of the following two approaches.

  • Create a new subclass of UploadServiceBroadcastReceiver inside a Service or Activity. For detailed instructions on how to to register and unregister the broadcast receiver, refer to the register and unregister method javadocs.

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

    @Override
    public void onCancelled(UploadInfo uploadInfo) {
        // Context context = getReceiverContext();
        // your code here
    }

    @Override
    public void onError(UploadInfo uploadInfo, Exception exception) {
        // Context context = getReceiverContext();
        // your code here
    }

    @Override
    public void onCompleted(UploadInfo uploadInfo, ServerResponse serverResponse) {
        // Context context = getReceiverContext();
        // 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) {
        // Context context = getReceiverContext();
        // your code here
    }

    @Override
    public void onError(UploadInfo uploadInfo, Exception exception) {
        // Context context = getReceiverContext();
        // your code here
    }

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