如何显示剩余的KB文件将在android的进度条中下载。 例如剩下12kb / 120kb ......然后是97kb / 120kb ......等等
我们是否可以拥有此进度对话框,如图像
所示答案 0 :(得分:2)
class DownloadFileAsync extends AsyncTask<String, String, String> {
private ProgressDialog mProgressDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(UrlTestActivity.this);
mProgressDialog.setMessage("Downloading file..");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(false);
mProgressDialog.show();
}
@Override
protected String doInBackground(String... aurl) {
int count;
try {
for (int i = 0; i < 3; i++) {
URL url = new URL("http://nodeload.github.com/nexes/Android-File-Manager/zipball/master");
URLConnection conexion = url.openConnection();
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
InputStream is = url.openStream();
File testDirectory = new File(Environment.getExternalStorageDirectory() + "/Folder");
if (!testDirectory.exists()) {
testDirectory.mkdir();
}
FileOutputStream fos = new FileOutputStream(testDirectory+ "/"+(i+100)+".zip");
byte data[] = new byte[1024];
long total = 0;
int progress = 0;
while ((count = is.read(data)) != -1) {
total += count;
int progress_temp = (int) total * 100 / lenghtOfFile;
publishProgress(""+ progress_temp);
if (progress_temp % 10 == 0 && progress != progress_temp) {
progress = progress_temp;
}
fos.write(data, 0, count);
}
is.close();
fos.close();
}
} catch (Exception e) {}
return null;
}
protected void onProgressUpdate(String... progress) {
Log.d("ANDRO_ASYNC",progress[0]);
mProgressDialog.setProgress(Integer.parseInt(progress[0]));
}
@Override
protected void onPostExecute(String unused) {
dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
}
}
答案 1 :(得分:1)
您所描述的最佳方式是使用AsyncTask执行下载。在onProgressUpdate方法中,您可以更新ProgressDialog以指示用户完成百分比。它看起来像这样:
答案 2 :(得分:1)
progressDialog.setMax(fileLength/1000);
publishProgress(String.valueOf(total /1000));
以下代码仅在API 11
之后可用progressDialog. setProgressNumberFormat ("%1d kb of %2d kb");
答案 3 :(得分:0)
答案 4 :(得分:0)
您可以使用进度对话框,如文档中所述。或者(也许更优雅),您可以在活动的标题栏中使用进度指示器。在您的活动中,在致电setContentView
之前,请添加以下行:
requestWindowFeature(Window.FEATURE_PROGRESS);
然后通过拨打setProgress(int)
,您可以指出进度。当进度达到10000时,标题栏中的进度指示器将消失。请注意,如果您确定非UI线程的进度,则应使用处理程序来调用setProgress
。