[問題]下載多個檔案使用ProgressDialog問題

作者: Dong0129 (阿東跟上面講的情況一樣b)   2018-08-21 08:09:53
各位版友好,
最近在寫透過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();
}
}
作者: andy2151 (阿鴻)   2018-08-21 22:10:00
異步執行你有了解運作原理嗎看起來你在主程式的for一次就把所有下載同時進行你要在第一個異步執行完成後在進行第二個下載建立一個變數downloadCount記住目前下載到哪 在onPostExecute中 downloadCount++;dowbliadFile(urls [downloadCount]);
作者: zcbxvsdf (東北一頭羊)   2018-08-22 22:08:00
只開一個thread跑所有download,用Handler 傳遞message
作者: t52101t (五子棋)   2018-08-22 22:49:00
把urls直接傳到asynctask裡面再用迴圈依次下載不好嗎
作者: baobomb (baobomb)   2018-09-01 16:04:00
建議用rxJava 會比較容易處理這種資料流的event

Links booklink

Contact Us: admin [ a t ] ucptt.com