作者Dong0129 (阿东)
看板AndroidDev
标题[问题]下载多个档案使用ProgressDialog问题
时间Tue Aug 21 08:09:53 2018
各位版友好,
最近在写透过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), 来自: 36.227.181.38
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/AndroidDev/M.1534810195.A.020.html
1F:推 andy2151: 异步执行你有了解运作原理吗08/21 22:10
2F:→ andy2151: 看起来你在主程式的for一次就把所有下载同时进行08/21 22:11
是的,程式运作是非同步所以第二个ProgressDialog弹出来後它就收不到download task
的
资料,因为第2个档案已经下载完了...我想请问是否有方法能够达成我要的效果呢?
※ 编辑: Dong0129 (36.227.181.38), 08/21/2018 23:19:33
3F:推 andy2151: 你要在第一个异步执行完成後在进行第二个下载08/22 01:19
4F:推 andy2151: 建立一个变数downloadCount08/22 01:24
5F:→ andy2151: 记住目前下载到哪 在onPostExecute中 downloadCount++;08/22 01:24
6F:→ andy2151: dowbliadFile(urls [downloadCount]);08/22 01:24
已成功,原来一开始想的方向就错了,谢谢!
7F:→ zcbxvsdf: 只开一个thread跑所有download,用Handler 传递message08/22 22:08
哈,这是另一种写法,是抓download id去算档案下载进度吧!
这两种写法的差异在哪呢?
8F:推 t52101t: 把urls直接传到asynctask里面再用回圈依次下载不好吗08/22 22:49
也是可行,一开始我想到的方向有点偏了,谢谢
※ 编辑: Dong0129 (114.137.240.236), 08/23/2018 09:35:02
9F:推 baobomb: 建议用rxJava 会比较容易处理这种资料流的event 09/01 16:04