首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >显示下载进度

显示下载进度
EN

Stack Overflow用户
提问于 2013-10-08 23:22:29
回答 1查看 107关注 0票数 0

我确实读过类似的问题,但对我来说都不起作用。问题是方法getProgress()返回非常大的值(例如16300 ).What我做错了吗?

请参阅我的代码

代码语言:javascript
复制
 public void download()
{
    RandomAccessFile file = null;
    InputStream inputStream = null;
    HttpURLConnection connection = null;
    try
    {
        connection = (HttpURLConnection)url.openConnection();
        connection.setRequestMethod("GET");
        connection.setDoOutput(true);
        connection.connect();
        String connectLength = connection.getHeaderField("Content-Length");

        if(connectLength == null || Integer.parseInt(connectLength) < 1 || connectLength.equals(""))
            error();

        size = Long.parseLong(connectLength);
        file = new RandomAccessFile(filePath,"rw");
        inputStream = new BufferedInputStream(url.openStream());
        int read ;

        while((read = inputStream.read(buffer)) > 0 && status == States.DOWNLOADING)
        {
            file.write(buffer,0,read);
            downloadedS += read;
        }
    }
}
 public float getProgress()
    { 
        return ((float)downloadedS/size)*100;
    }
EN

回答 1

Stack Overflow用户

发布于 2013-10-08 23:55:50

AsyncTask支持正确且轻松地使用UI线程。这个类允许在UI线程上执行后台操作和发布结果,而不必操作线程和/或处理程序。AsyncTask

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19251843

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档