【发布时间】:2014-02-27 10:26:39
【问题描述】:
我正在开发一个下载文件并显示 2 个进度条的应用,第一个显示当前下载文件,第二个显示基于文件数量的总进度。
我在我的应用程序中使用DoubleProgressBar 库:
我成功更新了第一个 ProgressBar,但卡住了第二个。
这是我的 AsyncTask 类代码:
private DoubleProgressDialog pDialog;
class DownloadFileFromURL extends AsyncTask<String, Integer, String> {
Context mContext;
public DownloadFileFromURL(Context ctx) {
// TODO Auto-generated constructor stub
this.mContext = ctx;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(CUSTOM_PROGRESS_DIALOG);
}
/* Downloading file in background thread */
@Override
protected String doInBackground(String... f_url) {
InputStream input = null;
OutputStream output = null;
HttpURLConnection connection = null;
try {
URL url = new URL(f_url[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
// getting file length
int fileLength = connection.getContentLength();
for (int i = 0; i <= ArrayOfFiles.length; i++){
File f = new File(Environment.getExternalStorageDirectory() + "/Folder/", ArrayOfFiles[i]);
// input stream to read file - with 8k buffer
input = new BufferedInputStream(url.openStream(), 8192);
// Output stream to write file
output = new FileOutputStream(f);
byte data[] = new byte[8192];
long total = 0;
int count;
int EntireProgress = 0;
while ((count = input.read(data)) != -1) {
// allow canceling with back button
if (isCancelled()) {
input.close();
return null;
}
total += count;
// publishing the progress....
if (fileLength > 0) // only if total length is known
publishProgress((int)(total * 100 / fileLength));
output.write(data, 0, count);
/*Here is my trouble, the 2nd ProgressBar is updating as the same of the first one, I need the 2nd one to update itself slowly till all files get downloaded*/
int CurrentProgress = pDialog.getProgress();
pDialog.setSecondaryProgress(CurrentProgress );
publishProgress(CurrentProgress );
}
} 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 onPostExecute(String result) {
// dismiss the dialog after the file was downloaded
dismissDialog(CUSTOM_PROGRESS_DIALOG);
if (result != null)
Toast.makeText(mContext,"Download error: " + result, Toast.LENGTH_LONG).show();
else
Toast.makeText(mContext,"File downloaded", Toast.LENGTH_SHORT).show();
}
@Override
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
// if we get here, length is known, now set indeterminate to false
pDialog.setIndeterminate(false);
pDialog.setMax(100);
pDialog.setProgress(progress[0]);
}
}
我也在我的活动类中使用了这个方法:
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case CUSTOM_PROGRESS_DIALOG:
pDialog = new DoubleProgressDialog(NetworkActivity.this);
pDialog.setMessage("Downloading file. Please wait...");
pDialog.setMax(100);
pDialog.setIndeterminate(true);
pDialog.setCancelable(false);
pDialog.setButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
pDialog.show();
return pDialog;
default:
return null;
}
}
有什么想法吗?
【问题讨论】:
-
publishProgress(CurrentProgress );这个方法有什么作用? ALSO:您只处理一个文件,但想根据多个文件更新第二个进度? -
尝试将
pDialog.setSecondaryProgress(CurrentProgress );移动到onProgressUpdate(Integer... progress)。由于您正在从可能是问题的非 UI 线程更新 UI -
@cosmincalistru ,可以从 doInBackground(Params...) 调用此方法,以便在后台计算仍在运行时在 UI 线程上发布更新。每次调用此方法都会触发 UI 线程上 onProgressUpdate(Progress...) 的执行。如果任务被取消,onProgressUpdate(Progress...) 将被调用。将更新我的代码以下载多个文件。
-
@super-qua ,它可以工作,但是一旦第一个文件下载,第二个 ProgressBar 会自行重置,然后它通常会再次与第二个文件一起计数,然后在完成时重置,任何想法,即如何保存第一个文件的当前进度并在第二个文件下载时加起来?
-
您在主
Activity中声明了“DownloadFileFromUrl”类?还是一个单独的类?
标签: android android-asynctask android-progressbar