进度条完成百分比(下载)在 Android 中显示 [英] Progress Bar % completed (download) Display in Android

查看:19
本文介绍了进度条完成百分比(下载)在 Android 中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到进度条显示下载完成百分比的问题.我有一个服务类,在其中进行下载.我可以从这个类中获取下载的开始时间和结束时间.在单击按钮的主要活动中,我必须显示进度百分比.我听说它可以通过 AsyncTask 实现,但我不知道它是如何工作的.请帮助我提供一些与此相关的示例代码示例.谢谢

I have a problem with Progress Bar showing % of download completed. I have a service class in which the downloading takes place. I can get the start time and end time of download from this class. In Main Activity on clicking a button I have to show the progress %. I heard that its possible through AsyncTask but I have no clue about how it works. Please help me with some sample code example related to this. Thanks

推荐答案

我更喜欢 AsyncTask.

I would prefer AsyncTask for this.

这是一个例子

ProgressDialog mProgressDialog;
// instantiate it within the onCreate method
mProgressDialog = new ProgressDialog(YourActivity.this);
mProgressDialog.setMessage("A message");
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

// execute this when the downloader must be fired
DownloadFile downloadFile = new DownloadFile();
downloadFile.execute("the url to the file you want to download");

这里是异步任务

private class DownloadFile extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... sUrl) {
    try {
        URL url = new URL(sUrl[0]);
        URLConnection connection = url.openConnection();
        connection.connect();
        // this will be useful so that you can show a typical 0-100% progress bar
        int fileLength = connection.getContentLength();

        // download the file
        InputStream input = new BufferedInputStream(url.openStream());
        OutputStream output = new FileOutputStream("/sdcard/file_name.extension");

        byte data[] = new byte[1024];
        long total = 0;
        int count;
        while ((count = input.read(data)) != -1) {
            total += count;
            // publishing the progress....
            publishProgress((int) (total * 100 / fileLength));
            output.write(data, 0, count);
        }

        output.flush();
        output.close();
        input.close();
    } catch (Exception e) {
    }
    return null;
}

这可以使用从服务下载以及下载管理器类来完成.有关详细信息.

This can be done using Download from Service as well as Download Manager class. Refer this question for details.

编辑

完成百分比是您在进度对话框中实际发布的百分比.如果你想显示百分比,你可以使用这个(total * 100/fileLength).

The percentage completed is what you actually publish in the progress dialog. If you want to display the percentage, you may use this (total * 100 / fileLength).

int percentage =  (total * 100 / fileLength);
TextView tv = (TextView)findViewById(R.id.textview);
tv.setText("" + percentage);

使用此代码在所需的文本视图中显示百分比.

Use this code to display the percentage in the desired textview.

这篇关于进度条完成百分比(下载)在 Android 中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆