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

作者: 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();
}
}
作者: aiweisen (艾維森)   2018-08-21 13:38:00
先用log.d檢查有沒有進去onProgressUpdate再檢查progress[0] return 是不是always 0
作者: frog79110 (尼姆)   2018-08-27 20:46:00
把for迴圈搬到doInBackground裡面做@@?
作者: y3k (激流を制するは静水)   2018-08-28 07:02:00
可以阿 最直接就是你按按鈕時是把新的Task Push進一個List 然後onPostExecute()時去看看還有沒有下一個要下載的 有的話直接拖出來跑另外這動作有java.util.Stack<?>()可以用

Links booklink

Contact Us: admin [ a t ] ucptt.com