如何获取上传进度

发布于 2024-12-27 13:22:20 字数 1678 浏览 2 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我的痛♀有谁懂 2025-01-03 13:22:20

您必须在 doInBackground(...) 方法中调用 publishProgress(...)。这就是导致 onProgressUpdate(...) 被调用的原因。 onProgressUpdate(...) 并不是神奇地调用的。

它被设计为多用途,您有责任通过 publishProgress(...) 使用您希望其发布的任何“进度”数据来触发它。它可以是数字,例如 10 代表 10%,也可以是字符串,例如 下载的第一个文件...

AsyncTask 类不知道您要发布什么内容或何时发布 - 这就是从 doInBackground(...)< 调用 publishProgress(...) 的内容/code> 的意思是这样做。

You have to call publishProgress(...) in your doInBackground(...) method. It's what causes onProgressUpdate(...) to be called. The onProgressUpdate(...) 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 as 10 for 10 percent or a string such as First file downloaded....

The AsyncTask class has no idea what you want to publish or when - that's what a call to publishProgress(...) from doInBackground(...) is meant to do.

终难遇 2025-01-03 13:22:20

您将需要创建第二个线程来跟踪进度并将其报告给此活动。以下链接包含有关如何完成此操作的示例:
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"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文