更新进度对话框 [英] Updating progress dialog

查看:142
本文介绍了更新进度对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让一个应用程序,可以帮助我评估,以从网络资源下载文件的时间。我发现2个样品:

I am trying to make an application that can help me to evaluate the time to download the file from a web resource. I have found 2 samples:

<一个href="http://stackoverflow.com/questions/3028306/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog">Download与Android的文件,并显示在一个ProgressDialog 进度

http://www.helloandroid.com/tutorials/如何下载-fileimage-URL-您的设备

第二个例子显示了一个更小的下载时间,但我不知道如何使用它来更新进度对话框。我认为有些事情应该,而EX pression在第二种情况下做到的,但我找不到什么。可能有人给我任何忠告?

The second example shows a smaller download time, but I cannot understand how to update progress dialog using it. I think something should be done with "while" expression in second case, but I cannot find what. Could someone give me any piece of advice?

UPD:

1日code:

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;

第2 code:

2nd code:

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

其中,

  public void DownloadFromUrl(String imageURL, String fileName) {  //this is the downloader method
 try {
         URL url = new URL(imageURL); //you can write here any link
         File file = new File(fileName);

        /*Open a connection to that URL. */
         URLConnection ucon = url.openConnection();

         /*
          * Define InputStreams to read from the URLConnection.
          */
         InputStream is = ucon.getInputStream();
         BufferedInputStream bis = new BufferedInputStream(is);

         /*
          * Read bytes to the Buffer until there is nothing more to read(-1).
          */
         ByteArrayBuffer baf = new ByteArrayBuffer(50);
         int current = 0;
         while ((current = bis.read()) != -1) {
                 baf.append((byte) current);
         }

         /* Convert the Bytes read to a String. */
         FileOutputStream fos = new FileOutputStream(PATH+file);
         fos.write(baf.toByteArray());
         fos.close();

 } catch (IOException e) {
         Log.d("ImageManager", "Error: " + e);
 }

所以事情是,第一种方法似乎是慢30%左右。

So the thing is that first method seems to be slower for about 30%.

推荐答案

的的第二个例子可以运行得更快,但它垄断了GUI线程。该第一种方法,使用的 的AsyncTask ,效果较好;它允许GUI留响应作为下载所得。

The second example may run faster, but it monopolizes the GUI thread. The first approach, using AsyncTask, is better; it allows the GUI to stay responsive as the download proceeds.

我觉得比较有帮助 的AsyncTask 的SwingWorker ,如本例如

I found it helpful to compare AsyncTask with SwingWorker, as shown in this example.

这篇关于更新进度对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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