Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 更新进度对话框_Java_Android_Dialog_Progress - Fatal编程技术网

Java 更新进度对话框

Java 更新进度对话框,java,android,dialog,progress,Java,Android,Dialog,Progress,我正在尝试制作一个应用程序,可以帮助我评估从web资源下载文件的时间。我发现了两个样本: 及 第二个示例显示了较小的下载时间,但我无法理解如何使用它更新进度对话框。我认为在第二种情况下应该用while表达式做些什么,但我找不到什么。有人能给我一些建议吗 UPD: 第1代码: try { time1 = System.currentTimeMillis(); URL url = new URL(path); URLConn

我正在尝试制作一个应用程序,可以帮助我评估从web资源下载文件的时间。我发现了两个样本:

第二个示例显示了较小的下载时间,但我无法理解如何使用它更新进度对话框。我认为在第二种情况下应该用while表达式做些什么,但我找不到什么。有人能给我一些建议吗

UPD:

第1代码:

try {
            time1 = System.currentTimeMillis();
            URL url = new URL(path);
            URLConnection conexion = url.openConnection();
            conexion.connect();
            // this will be useful so that you can show a tipical 0-100% progress bar
            int lenghtOfFile = conexion.getContentLength();
            // downlod the file
            InputStream input = new BufferedInputStream(url.openStream());
            OutputStream output = new FileOutputStream("/sdcard/analyzer/test.jpg");

            byte data[] = new byte[1024];

            long total = 0;

          time11 = System.currentTimeMillis();
           while ((count = input.read(data)) != -1) {
                total += count;
                // publishing the progress....
                publishProgress((int)(total*100/lenghtOfFile));
                output.write(data, 0, count);
            }
            time22= System.currentTimeMillis()-time11;
            output.flush();
            output.close();
            input.close();


        } catch (Exception e) {}

        timetaken = System.currentTimeMillis() - time1;
第二代码:

       long time1 = System.currentTimeMillis();
        DownloadFromUrl(path, "test.jpg");
        long timetaken = System.currentTimeMillis() - time1;
在哪里


所以问题是第一种方法似乎要慢30%左右。

第一个链接最好。但我不能在周一或以后提供代码(这是home comp),我可以提供完整的功能。但是:

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

            // downlod the file
            InputStream input = new BufferedInputStream(url.openStream());
            OutputStream output = new FileOutputStream("/sdcard/somewhere/nameofthefile.ext");

            byte data[] = new byte[1024];

            long total = 0;

            while ((count = input.read(data)) != -1) {
                total += count;
                // publishing the progress....
                publishProgress((int)(total*100/lenghtOfFile));
                output.write(data, 0, count);
            }

            output.flush();
            output.close();
            input.close();
        } catch (Exception e) {}
        return null;
    }
私有类下载文件扩展异步任务{
@凌驾
受保护的字符串doInBackground(字符串…url){
整数计数;
试一试{
URL=新URL(URL[0]);
URLConnection conexion=url.openConnection();
conexion.connect();
//这将非常有用,以便您可以显示典型的0-100%进度条
int lenghtOfFile=conexion.getContentLength();
//下载文件
InputStream输入=新的BufferedInputStream(url.openStream());
OutputStream output=新文件OutputStream(“/sdcard/somewhere/nameoffile.ext”);
字节数据[]=新字节[1024];
长总计=0;
而((计数=输入。读取(数据))!=-1){
总数+=计数;
//发布进度。。。。
出版进度(整数)(总计*100/长办公室);
输出.写入(数据,0,计数);
}
output.flush();
output.close();
input.close();
}捕获(例外e){}
返回null;
}
这个类最适合它(imho)。publishProgress这是一个简单的函数,其中最多有两行。设置最大值和设置当前值。如何在这个代码
长度文件
中看到你的文件有多少字节。
total
-当前进度(示例25,从100字节)。轻松运行这个类:
DownloadFile a=new DownloadFile();a.execute(value,value);//如果不使用value,则为null。
希望您理解我的意思,我的英语说得不好。

可能运行得更快,但它垄断了GUI线程。使用的更好;它允许GUI在下载过程中保持响应


我发现与进行比较很有帮助,如图所示。

我了解它的工作原理,但我发现使用第一个代码下载文件的速度比使用第二个代码慢大约30%。我不理解为什么。字节数据[]=新字节[1024];您需要在此处设置新值,例如16000。它提高了下载速度。我在Asynctask中执行第二个方法。第一个代码有1024字节的缓冲区,而第二个只有50字节。我尝试将缓冲区减少到50。但我没有看到性能上的任何变化。您说,“第一个代码我下载一个文件大约慢30%”,然后“第一种方法似乎要快30%左右。"这似乎是一个矛盾。我注意到,
AsyncTask
必须将其操作与GUI交错以显示进度,因此30%似乎是合理的。对不起,我已更正了错误。是否可以在更新进度对话框的同时向AsyncTask插入第二个代码?或者在这种情况下,我的性能也会下降?或者我可能会n提高第一个代码的性能?
private class DownloadFile extends AsyncTask<String, Integer, String>{
    @Override
    protected String doInBackground(String... url) {
        int count;
        try {
            URL url = new URL(url[0]);
            URLConnection conexion = url.openConnection();
            conexion.connect();
            // this will be useful so that you can show a tipical 0-100% progress bar
            int lenghtOfFile = conexion.getContentLength();

            // downlod the file
            InputStream input = new BufferedInputStream(url.openStream());
            OutputStream output = new FileOutputStream("/sdcard/somewhere/nameofthefile.ext");

            byte data[] = new byte[1024];

            long total = 0;

            while ((count = input.read(data)) != -1) {
                total += count;
                // publishing the progress....
                publishProgress((int)(total*100/lenghtOfFile));
                output.write(data, 0, count);
            }

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