我开始使用这个库,它显示了一个很好的上传进度条。 https://github.com/gotev/android-upload-service
按照文档我添加了一个BroadcastReceiver,但是,虽然上传作为服务运行并且更新得很好:
如果用户关闭了声明broadcastReceiver的活动,我将无法在上传结束时做出响应。如何保持对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");
}
};
答案 0 :(得分:0)
您可以创建一个新类(例如MyReceiver),它扩展UploadServiceBroadcastReceiver,在其中添加您的业务逻辑,然后将其注册为清单中的广播接收器(如下所示),并使用intent过滤器com.yourcompany.yourapp。 uploadservice.broadcast.status。通过这种方式,您可以独立于您的活动和服务来监听活动。使用初始设置中设置为UploadService.NAMESPACE的内容更改com.yourcompany.yourapp:
AndroidManifest.xml中的
<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(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
}
}
非常重要!如果您想使用UploadServiceBroadcastReceiver处理事件,请不要在任何地方调用setDelegate方法。
更多信息:https://github.com/gotev/android-upload-service/wiki/Monitoring%20upload%20status