-
-
Notifications
You must be signed in to change notification settings - Fork 694
Monitoring upload status
At any time, from anywhere in your code, you can call:
UploadService.getTaskList()
to see which upload tasks are in progress.
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(Context context, UploadInfo uploadInfo) { // your code here } @Override public void onError(Context context, UploadInfo uploadInfo, Exception exception) { // your code here } @Override public void onCompleted(Context context, UploadInfo uploadInfo, ServerResponse serverResponse) { // your code here // if you have mapped your server response to a POJO, you can easily get it: // YourClass obj = new Gson().fromJson(serverResponse.getBodyAsString(), YourClass.class); } @Override public void onCancelled(Context context, 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 thesetDelegate
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.
The recommended way to use delegates inside Activities or Services is to implement theUploadStatusDelegate
interface in your Activities or Services instead of using the anonymous inner class, to prevent variable out of scope problems.
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 the last of the proposed approaches to monitor status and to react to events. -
Create a new subclass of UploadServiceBroadcastReceiver inside a
Service
orActivity
. 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(Context context, UploadInfo uploadInfo) { // Context context = getReceiverContext(); // your code here } @Override public void onCancelled(Context context, UploadInfo uploadInfo) { // Context context = getReceiverContext(); // your code here } @Override public void onError(Context context, UploadInfo uploadInfo, Exception exception) { // Context context = getReceiverContext(); // your code here } @Override public void onCompleted(Context context, 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. Changecom.yourcompany.yourapp
with whatever you have set asUploadService.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
public class MyReceiver extends UploadServiceBroadcastReceiver {
@Override
public void onProgress(Context context, UploadInfo uploadInfo) {
// your code here
}
@Override
public void onCancelled(Context context, UploadInfo uploadInfo) {
// Context context = getReceiverContext();
// your code here
}
@Override
public void onError(Context context, UploadInfo uploadInfo, Exception exception) {
// Context context = getReceiverContext();
// your code here
}
@Override
public void onCompleted(Context context, UploadInfo uploadInfo, ServerResponse serverResponse) {
// Context context = getReceiverContext();
// your code here
}
}
There are times when you want to monitor only one particular upload task using a broadcast receiver. This can come handy to prevent you from writing some boilerplate code:
public class SingleUploadBroadcastReceiver extends UploadServiceBroadcastReceiver {
private String mUploadID;
private UploadStatusDelegate mDelegate;
public void setUploadID(String uploadID) {
mUploadID = uploadID;
}
public void setDelegate(UploadStatusDelegate delegate) {
mDelegate = delegate;
}
public SingleUploadBroadcastReceiver() {
super();
}
@Override
public void onProgress(Context context, UploadInfo uploadInfo) {
mDelegate.onProgress(context, uploadInfo);
}
@Override
public void onError(Context context, UploadInfo uploadInfo, Exception exception) {
mDelegate.onError(context, uploadInfo, exception);
}
@Override
public void onCompleted(Context context, UploadInfo uploadInfo, ServerResponse serverResponse) {
mDelegate.onCompleted(context, uploadInfo, serverResponse);
}
@Override
public void onCancelled(Context context, UploadInfo uploadInfo) {
mDelegate.onCancelled(context, uploadInfo);
}
}