楼主: 
Dong0129 (阿东跟上面讲的情况一样b)   
2018-08-21 00:11:04各位版友好,
最近在写透过ProgressDialog依序显示多个URL下载档案的APP时有个问题
一直不知道该怎么解,程式码及叙述如下...
将多个url存入一个array list中,依序将这些url丢入function中可透过Progress Dialo
g
让使用者可以看到每个档案的下载进度,每个档案下载完后,Progress Dialog关闭,
等到下一个url被传入downloadFile(url)时,再开启一个Progress Dialog显示进度条,
但目前只有第一笔url会正常显示进度条,第二笔url的进度条一直停留在0%,但事实上
档案有被下载到目的地中...请问是否哪里写错了呢?
in Main:
for(String url:urls)
{
    downloadFile(url);
}
downloadFile() function:
private  void download(String data)
{
    app_name=data
             .substring(data.indexOf("0%2F"),data.indexOf("apk"))
             .replace("0%2F","")
             .replace(".",".apk");
    final DownloadTask downloadTask = new DownloadTask(MainActivity.this);
    downloadTask.execute(data);
    mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener()
 {
            @Override
            public void onCancel(DialogInterface dialog) {
                downloadTask.cancel(true);
            }
        });
}
private class DownloadTask extends AsyncTask<String, Integer, String>
{
    private Context context;
    private PowerManager.WakeLock mWakeLock;
    public DownloadTask(Context context)
    {
        this.context = context;
    }
    @Override
    protected String doInBackground(String... sUrl)
    {
        InputStream input = null;
        OutputStream output = null;
        HttpURLConnection connection = null;
        try {
            URL url = new URL(sUrl[0]);
            Log.i("Eden","surl:"+sUrl[0]);
            app_name=sUrl[0]
                     .substring(sUrl[0].indexOf("0%2F"),sUrl[0].indexOf("apk")
)
                     .replace("0%2F","")
                     .replace(".",".apk");
            connection = (HttpURLConnection) url.openConnection();
            connection.connect();
            if (connection.getResponseCode() != HttpURLConnection.HTTP_OK)
            {
                return "Server returned HTTP "
                      + connection.getResponseCode()
                      + " "
                      + connection.getResponseMessage();
            }
            int fileLength = connection.getContentLength();
            // download the file
            input = connection.getInputStream();
            output = new FileOutputStream("/storage/emulated/0/Download/"
                                          +app_name);
            byte data[] = new byte[4096];
            long total = 0;
            int count;
            while ((count = input.read(data)) != -1)
            {
                if (isCancelled())
                {
                    input.close();
                    return null;
                }
                total += count;
                if (fileLength > 0)
                    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;
        }
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            mProgressDialog = new ProgressDialog(MainActivity.this);
            mProgressDialog.setTitle("Downloading...");
            mProgressDialog.setMessage("Downloading "+app_name);
            mProgressDialog.setIndeterminate(false);
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            mProgressDialog.setCancelable(true);
            mProgressDialog.setMax(100);
            PowerManager pm = (PowerManager) context
                                     .getSystemService(Context.POWER_SERVICE);
            mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                                       getClass().getName());
            mWakeLock.acquire();
            mProgressDialog.show();
        }
        @Override
        protected void onProgressUpdate(Integer... progress) {
            super.onProgressUpdate(progress);
            // if we get here, length is known, now set indeterminate to false
            mProgressDialog.setProgress(progress[0]);
        }
        @Override
        protected void onPostExecute(String result) {
            mWakeLock.release();
            mProgressDialog.dismiss();
            if (result != null)
                Toast.makeText(context,"Download error: "+result,
                               Toast.LENGTH_LONG).show();
            else
                Toast.makeText(context,"File downloaded",
                               Toast.LENGTH_SHORT).show();
        }
}