看板 java 關於我們 聯絡資訊
各位版友好, 最近在寫透過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(); } } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 220.137.81.124 ※ 文章網址: https://www.ptt.cc/bbs/java/M.1534781466.A.24F.html
aiweisen: 先用log.d檢查有沒有進去onProgressUpdate08/21 13:38
aiweisen: 再檢查progress[0] return 是不是always 008/21 13:39
因為第1個檔案還沒下載完,第2個檔案就跟著下載了,我想請問的是,是否有方法可以 等第1個檔案下載完後,第1個檔案的ProgressDialog也dismiss了之後,才接著下載下1個 檔案,並彈出新的ProgressDialog顯示進度條...請各位版友指點.. ※ 編輯: Dong0129 (36.227.181.38), 08/21/2018 23:23:10
frog79110: 把for迴圈搬到doInBackground裡面做@@?08/27 20:46
y3k: 可以阿 最直接就是你按按鈕時是把新的Task Push進一個List 然08/28 07:02
y3k: 後onPostExecute()時去看看還有沒有下一個要下載的 有的話08/28 07:03
y3k: 直接拖出來跑08/28 07:03
y3k: 另外這動作有java.util.Stack<?>()可以用08/28 07:05
哈,已經解出來了,謝謝大家的回覆喔! ※ 編輯: Dong0129 (42.73.20.218), 08/28/2018 18:59:05