我開始使用這個庫,它顯示了一個很好的上傳進度條。 https://github.com/gotev/android-upload-serviceUploadServiceBroadcastReceiver在活動結束時關閉
繼我加了一個廣播接收器,不過,雖然上傳作爲服務運行和精美更新文檔:
如果用戶關閉其中的BroadcastReceiver被宣佈我的活動在上傳結束後放棄回覆的能力。我能做些什麼來保持對broadcastReceiver的引用?
注意:我嘗試將接收器移動到單獨的單例類,但是這並沒有解決問題。
的廣播接收器:
public 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));
String response = new String(serverResponseBody);
dialog.dismiss();
//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");
}
};