-
-
Notifications
You must be signed in to change notification settings - Fork 697
Monitoring upload status
To listen for the status of the upload tasks, use the provided UploadServiceBroadcastReceiver in one of the following ways:
-
use it inside a
Service
orActivity
(check register and unregister methods JavaDoc for detailed instructions) -
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 filtercom.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>
Override its methods to add your own business logic.
Example on how to use it in an activity:
public class YourActivity extends Activity {
private static final String TAG = "AndroidUploadService";
private final UploadServiceBroadcastReceiver uploadReceiver =
new UploadServiceBroadcastReceiver() {
// you can override this progress method if you want to get
// the completion progress in percent (0 to 100)
// or if you need to know exactly how many bytes have been transferred
// override the method below this one
@Override
public void onProgress(String uploadId, int progress) {
Log.i(TAG, "The progress of the upload with ID "
+ uploadId + " is: " + progress);
}
@Override
public void onProgress(final String uploadId,
final long uploadedBytes,
final long totalBytes) {
Log.i(TAG, "Upload with ID " + uploadId +
" uploaded bytes: " + uploadedBytes
+ ", total: " + totalBytes);
}
@Override
public void onError(String uploadId, Exception exception) {
Log.e(TAG, "Error in upload with ID: " + uploadId + ". "
+ exception.getLocalizedMessage(), exception);
}
@Override
public void onCompleted(String uploadId,
int serverResponseCode,
byte[] serverResponseBody) {
// At this point, the serverResponseBody has been completely downloaded
// and is cached in memory, so no NetworkOnMainThread could happen here
Log.i(TAG, "Upload with ID " + uploadId
+ " has been completed with HTTP " + serverResponseCode
+ ". Response from server: "
+ new String(serverResponseBody));
//If your server responds with a JSON, you can parse it
//from serverResponseBody using a library
//such as org.json (embedded in Android) or Google's gson
}
@Override
public void onCancelled(String uploadId) {
Log.i(TAG, "Upload with ID " + uploadId
+ " has been cancelled by the user");
}
};
@Override
protected void onResume() {
super.onResume();
uploadReceiver.register(this);
}
@Override
protected void onPause() {
super.onPause();
uploadReceiver.unregister(this);
}
}
If you want to monitor upload status in all of your activities, just implement the UploadServiceBroadcastReceiver in your base activity class from which all of your activities inherits and you're done.
There are times when you want to monitor a single upload. You can of course monitor the status as described above, but you can do it in a more convenient way. First of all, create a new class in your project and copy-paste this snippet.
public class SingleUploadBroadcastReceiver extends UploadServiceBroadcastReceiver {
public interface Delegate {
void onProgress(int progress);
void onProgress(long uploadedBytes, long totalBytes);
void onError(Exception exception);
void onCompleted(int serverResponseCode, byte[] serverResponseBody);
void onCancelled();
}
private String mUploadID;
private Delegate mDelegate;
public void setUploadID(String uploadID) {
mUploadID = uploadID;
}
public void setDelegate(Delegate delegate) {
mDelegate = delegate;
}
@Override
public void onProgress(String uploadId, int progress) {
if (uploadId.equals(mUploadID) && mDelegate != null) {
mDelegate.onProgress(progress);
}
}
@Override
public void onProgress(String uploadId, long uploadedBytes, long totalBytes) {
if (uploadId.equals(mUploadID) && mDelegate != null) {
mDelegate.onProgress(uploadedBytes, totalBytes);
}
}
@Override
public void onError(String uploadId, Exception exception) {
if (uploadId.equals(mUploadID) && mDelegate != null) {
mDelegate.onError(exception);
}
}
@Override
public void onCompleted(String uploadId, int serverResponseCode, byte[] serverResponseBody) {
if (uploadId.equals(mUploadID) && mDelegate != null) {
mDelegate.onCompleted(serverResponseCode, serverResponseBody);
}
}
@Override
public void onCancelled(String uploadId) {
if (uploadId.equals(mUploadID) && mDelegate != null) {
mDelegate.onCancelled();
}
}
}
Then, in your activity:
public class YourActivity extends Activity implements SingleUploadBroadcastReceiver.Delegate {
private static final String TAG = "AndroidUploadService";
private final SingleUploadBroadcastReceiver uploadReceiver =
new SingleUploadBroadcastReceiver();
@Override
protected void onResume() {
super.onResume();
uploadReceiver.register(this);
}
@Override
protected void onPause() {
super.onPause();
uploadReceiver.unregister(this);
}
public void uploadMultipart(final Context context) {
try {
String uploadId = UUID.randomUUID().toString();
uploadReceiver.setDelegate(this);
uploadReceiver.setUploadID(uploadId);
new MultipartUploadRequest(context, uploadId, "http://upload.server.com/path")
.addFileToUpload("/absolute/path/to/your/file", "your-param-name")
.setNotificationConfig(new UploadNotificationConfig())
.setMaxRetries(2)
.startUpload();
} catch (Exception exc) {
Log.e(TAG, exc.getMessage(), exc);
}
}
@Override
public void onProgress(int progress) {
//your implementation
}
@Override
public void onProgress(long uploadedBytes, long totalBytes) {
//your implementation
}
@Override
public void onError(Exception exception) {
//your implementation
}
@Override
public void onCompleted(int serverResponseCode, byte[] serverResponseBody) {
//your implementation
}
@Override
public void onCancelled() {
//your implementation
}
}