0
A
回答
1
在AsyncTask,后台任务将xml保存在本地内存。
int count;
try {
URL url = new URL("http://sample.com/test");
URLConnection conection = url.openConnection();
conection.connect();
// getting file length
int lenghtOfFile = conection.getContentLength();
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(), 8192);
// Output stream to write file
OutputStream output = new FileOutputStream("/data/data/com.pc.demo/temp.xml");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
int progress = (int)((total*100)/lenghtOfFile) ;
System.out.println("update---"+progress);
publishProgress(progress);
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
onPostExecute
File yourFile = new File("/data/data/com.pc.demo/temp.xml");
InputStream input = new BufferedInputStream(new FileInputStream(yourFile), 8086);
onProgressUpdate
setProgressPercent(progress[0])
0
您可以覆盖你的AsyncTask的onProgressUpdate()函数,并在doInBackgroud使用publishProgress(进展...值)( )把你的保存状态推到进度然后更新它,这里是我简单的代码,愿望可以帮助你。
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i/(float) count) * 100));
// Escape early if cancel() is called
if (isCancelled()) break;
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]); //change this with your progress bar
}
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
}
+0
谢谢,我已经更新了答案 – 2013-04-25 05:51:07
相关问题
- 1. 如何在android中显示进度
- 2. 如何在android中启动应用程序时显示进度
- 3. 如何在android中连接服务器时显示进度条?
- 4. 如何在WPF进度条中Indeterminate =“True”时显示进度?
- 5. 如何在阅读XML文件时显示进度条?
- 6. 如何在android中的titleBar中显示进度指示器或进度条?
- 7. 如何在BlackBerry中显示进度条?
- 8. 如何在Windows 7任务栏中显示进度(使用Qt)?
- 9. 如何显示长时间运行的Ansible任务的进度?
- 10. 如何从远程URL获取视频/图像时显示进度对话框?
- 11. 如何显示sftp进度
- 12. 如何在时间进度条中放置时间显示框
- 13. 在Android中的异步任务中显示进度条
- 14. 如何在android中显示额外的20秒进度条?
- 15. 如何在android中隐藏和显示进度条?
- 16. 如何在android中使listview显示进度条?
- 17. 如何在Android中显示进度对话框?
- 18. 如何在android中显示进度控制或gif?
- 19. 如何在android中显示动作栏上的进度条?
- 20. Android:如何在中心显示全屏进度条
- 21. 如何在android中显示进度条/圆圈
- 22. 在jsp中,如何在上传文件时显示进度条
- 23. 如何在Android上传图片时在图片视图中显示进度条?
- 24. 如何在任何时候显示DBGrid时获取方法?
- 25. 如何在webview上显示进度条?
- 26. 如何在PHP中更新时显示进度消息?
- 27. Android:显示倒计时的进度条?
- 28. 如何在JQuery数据读取器中显示小时数
- 29. 如何在阅读多个文本文件时显示进度条
- 30. 如何读取URL
+1自我回答 – 2013-04-29 08:35:52