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中將web url加載到webview時顯示進度?
- 3. 如何在android中啓動應用程序時顯示進度
- 4. 如何在android中連接服務器時顯示進度條?
- 5. 如何在WPF進度條中Indeterminate =「True」時顯示進度?
- 6. 如何在閱讀XML文件時顯示進度條?
- 7. 如何在android中的titleBar中顯示進度指示器或進度條?
- 8. Android - 如何在服務運行時顯示進度對話框?
- 9. 如何在加載新的android碎片時顯示進度條?
- 10. 如何讓任務欄在PyQt4中顯示我的進度條?
- 11. 如何在Windows 7任務欄中顯示進度(使用Qt)?
- 12. 如何在搖動中顯示進度?
- 13. 如何在BlackBerry中顯示進度條?
- 14. 如何在JProgressBar中顯示讀/寫serilization的進度?
- 15. 如何顯示長時間運行的Ansible任務的進度?
- 16. 如何從遠程URL獲取視頻/圖像時顯示進度對話框?
- 17. PHPUnit如何顯示進度?
- 18. 如何顯示進度?
- 19. 如何顯示sftp進度
- 20. 如何顯示進度條?
- 21. 如何從URL讀取XML在Android的
- 22. 如何在時間進度條中放置時間顯示框
- 23. 執行任務時顯示進度條
- 24. 在Android中的異步任務中顯示進度條
- 25. 如何在android中顯示進度條/圓圈
- 26. 如何在android中顯示進度控制或gif?
- 27. 如何在android中隱藏和顯示進度條?
- 28. 如何在webview中顯示和隱藏進度條 - android
- 29. 如何在android中顯示額外的20秒進度條?
- 30. Android:如何在中心顯示全屏進度條
+1自我回答 – 2013-04-29 08:35:52