java 板


LINE

各位版友好, 最近在写透过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://webptt.com/cn.aspx?n=bbs/java/M.1534781466.A.24F.html
1F:推 aiweisen: 先用log.d检查有没有进去onProgressUpdate08/21 13:38
2F:→ 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
3F:推 frog79110: 把for回圈搬到doInBackground里面做@@?08/27 20:46
4F:→ y3k: 可以阿 最直接就是你按按钮时是把新的Task Push进一个List 然08/28 07:02
5F:→ y3k: 後onPostExecute()时去看看还有没有下一个要下载的 有的话08/28 07:03
6F:→ y3k: 直接拖出来跑08/28 07:03
7F:→ y3k: 另外这动作有java.util.Stack<?>()可以用08/28 07:05
哈,已经解出来了,谢谢大家的回覆喔! ※ 编辑: Dong0129 (42.73.20.218), 08/28/2018 18:59:05







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Gossiping站内搜寻

TOP