如何获取上传进度
我使用 doInBackGround 和 AsyncTask 方法来显示进度,如下所示:
class UploadFile extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(DIALOG_DOWNLOAD_PROGRESS);
}
@Override
protected String doInBackground(String... aurl) {
upload(ActionUrl, uploadFile, savepath, newName);
return null;
}
protected void onProgressUpdate(String... progress) {
mProgressDialog.setProgress(Integer.parseInt(progress[0]));
}
@Override
protected void onPostExecute(String unused) {
dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
}
}
upload(ActionUrl, uploadFile, savepath, newName);
方法例如 这个。 和下面的代码:
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_DOWNLOAD_PROGRESS: //we set this to 0
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Downloading file...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(true);
mProgressDialog.show();
return mProgressDialog;
default:
return null;
}
}
和下面的调用:
private ProgressDialog mProgressDialog;
public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
new UploadFile().execute("test");
但进度对话框总是显示 0%,并且从不更新。 怎样修改才能更新呢?
I use doInBackGround and AsyncTask method to show progress as below:
class UploadFile extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(DIALOG_DOWNLOAD_PROGRESS);
}
@Override
protected String doInBackground(String... aurl) {
upload(ActionUrl, uploadFile, savepath, newName);
return null;
}
protected void onProgressUpdate(String... progress) {
mProgressDialog.setProgress(Integer.parseInt(progress[0]));
}
@Override
protected void onPostExecute(String unused) {
dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
}
}
The upload(ActionUrl, uploadFile, savepath, newName);
method is such as this.
And below code:
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_DOWNLOAD_PROGRESS: //we set this to 0
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Downloading file...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(true);
mProgressDialog.show();
return mProgressDialog;
default:
return null;
}
}
And below to call:
private ProgressDialog mProgressDialog;
public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
new UploadFile().execute("test");
But the Progress Dialog always show 0%, and never update.
How to modify to update it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须在
doInBackground(...)
方法中调用publishProgress(...)
。这就是导致onProgressUpdate(...)
被调用的原因。onProgressUpdate(...)
并不是神奇地调用的。它被设计为多用途,您有责任通过
publishProgress(...)
使用您希望其发布的任何“进度”数据来触发它。它可以是数字,例如10
代表 10%,也可以是字符串,例如下载的第一个文件...
。AsyncTask
类不知道您要发布什么内容或何时发布 - 这就是从doInBackground(...)< 调用
publishProgress(...)
的内容/code> 的意思是这样做。You have to call
publishProgress(...)
in yourdoInBackground(...)
method. It's what causesonProgressUpdate(...)
to be called. TheonProgressUpdate(...)
isn't called magically.It's designed to be multi-purpose and it's your responsibility to trigger it through
publishProgress(...)
with whatever 'progress' data you want it to publish. It can be numeric such as10
for 10 percent or a string such asFirst file downloaded...
.The
AsyncTask
class has no idea what you want to publish or when - that's what a call topublishProgress(...)
fromdoInBackground(...)
is meant to do.您将需要创建第二个线程来跟踪进度并将其报告给此活动。以下链接包含有关如何完成此操作的示例:
http://developer.android.com/guide/topics/ui/dialogs.html
在此链接中查找“带有第二个线程的示例 ProgressDialog”
you will need to create the second thread which will track the progress and report it to this activity.Here is the link which has an example on how to accomplish this:
http://developer.android.com/guide/topics/ui/dialogs.html
In this link look for "Example ProgressDialog with a second thread"