我通过 NotificationManager 发布文件上传进度,但在更新其进度 UI 时冻结。
我使用缓存在类字段中的 NotificationCompat.Builder。所以进度发布很简单:
manager.notify(id, uploader.
setProgress(MAX_PROGRESS, (int) (progress * 100), false).
build()
);
更新进度保证从主线程执行(包装在 Handler 装饰器中)。
this.request.setCallback(new UploaderDecorator(this.request.getCallback()));
进度公布如下:
long total = file.length();
long uploaded = 0;
int bytesRead = input.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
output.write(buffer, 0, bufferSize);
uploaded += bytesRead;
callback.onUploadProgress(activeFile, ((float) uploaded / total));
bytesRead = input.read(buffer, 0, bufferSize);
}
那么为什么它的工作这么慢呢?