[问题]下载多个档案使用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