-
-
Notifications
You must be signed in to change notification settings - Fork 697
Monitoring upload status
Alex Gotev edited this page Feb 14, 2016
·
41 revisions
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
(checkregister
andunregister
methods JavaDoc for detailed instructions) - create a new class (e.g. MyReceiver) which extends
UploadServiceBroadcastReceiver
and then register it as a broadcast receiver in your manifest, with the intent filtercom.yourcompany.yourapp.uploadservice.broadcast.status
. 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.