簡體   English   中英

AsyncTask的ProgressDialog

[英]ProgressDialog of AsyncTask

嗨,我需要顯示AsyncTask的摘要進度。 我想顯示正在下載的ProgressBar或ProgressDialog,但是我知道該怎么做,我知道如何顯示對話框,但是僅當我下載一個文件時才知道,而當我有很多文件要下載時該怎么辦。 有人可以幫忙嗎???

這是我的AyncTaskClass

public class DownloadProgramTask extends AsyncTask<String, Integer, String> {

    @Override
    protected String doInBackground(String... sUrl) {
        InputStream input = null;
        OutputStream output = null;
        HttpURLConnection connection = null;
        try {
            URL url = new URL(sUrl[0]);
            String path = sUrl[1];
            connection = (HttpURLConnection) url.openConnection();
            connection.connect();

            // expect HTTP 200 OK, so we don't mistakenly save error report
            // instead of the file
            if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
                return "Server returned HTTP " + connection.getResponseCode()
                        + " " + connection.getResponseMessage();
            }

            // this will be useful to display download percentage
            // might be -1: server did not report the length
            int fileLength = connection.getContentLength();

            // download the file
            input = connection.getInputStream();
            output = new FileOutputStream(path);

            byte data[] = new byte[4096];
            long total = 0;
            int count;
            while ((count = input.read(data)) != -1) {
                // allow canceling with back button
                if (isCancelled()) {
                    input.close();
                    return null;
                }
                total += count;
                // publishing the progress....
                if (fileLength > 0) // only if total length is known
                    publishProgress((int) (total * 100 / fileLength));
                output.write(data, 0, count);
            }
        } catch (Exception e) {
            return e.toString();
        } finally {
            try {
                if (output != null)
                    output.close();
                if (input != null)
                    input.close();
            } catch (IOException ignored) {
            }

            if (connection != null)
                connection.disconnect();
        }
        return null;
    }


}

我為每次執行創建該類的新實例,

File voiceFile = new File(dirVoice, a.getPose_id() + ".mp3");

            if (!voiceFile.exists()) {
                voiceList.add(a.getVoice());
                new DownloadProgramTask().execute(MYurl.BASE_URL + a.getVoice(), voiceFile.getPath());
                Log.e("LINK", MYurl.BASE_URL + a.getVoice());
                Log.e("Path voice", "" + voiceFile.getPath());

            }

            File imgLargeFile = new File(dirImageLarge, a.getId() + ".png");

            if (!imgLargeFile.exists()) {
                imgLargeList.add(a.getVoice());
                new DownloadProgramTask().execute(MYurl.BASE_URL + "/" + a.getImgLarge(), imgLargeFile.getPath());
            }

您可以使用異步任務的重寫方法OnProgressUpdate。 使用interger在異步任務的doingBackground中調用publishProgress

像這樣

@Override

    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
        Log.i("makemachine", "onProgressUpdate(): " + 
        percentBar.setProgress((values[0] * 2) + "%");

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM