DownloadManager를 사용하여 URL에서 xml 파일을 다운로드합니다. 그런 다음 스레드를 사용하여 파일을 SD 카드에 저장하는 데 2 초 정도 기다립니다.DownloadManager 및 Android에서 휴면 스레드로 다운로드하는 동안 ProgressBar
here과 같이 활동 동그라미를하고 싶습니다. 이것을 실현하는 가장 쉬운 방법은 무엇입니까? AsyncTask
을 구현해야합니까?
다운로드 기다리는 내 코드 :
//Download XML file from URL
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(URL));
request.setTitle("Download von "+Name+".xml");
// in order for this if to run, you must use the android 3.2 to compile your app
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir(FileSeperator+"XML"+FileSeperator, Name + FileExtension);
// get download service and enqueue file
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
File file = new File(Environment.getExternalStorageDirectory()+ FileSeperator
+"XML"+FileSeperator+ Name + FileExtension);
System.out.println("File existiert "+file.exists());
//insert delay after download to finish save progress before starting to parse the xml
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
UPDATE 내 AsyncTask를
private class DownloadFile extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... sUrl) {
try {
//Download XML file from URL
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(URL));
request.setTitle("Download von "+Name+".xml");
// in order for this if to run, you must use the android 3.2 to compile your app
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir(FileSeperator+"XML"+FileSeperator, Name + FileExtension);
// get download service and enqueue file
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
File file = new File(Environment.getExternalStorageDirectory()+ FileSeperator
+"XML"+FileSeperator+ Name + FileExtension);
System.out.println("File existiert "+file.exists());
//insert delay after download to finish save progress before starting to parse the xml
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (Exception e) {
}
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog.show();
}
protected void onPostExecute() {
super.onPreExecute();
pDialog.dismiss();
}
@Override
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
}
}
그리고 내가 그 같이 호출을 구현 여기 :
// instantiate it within the onCreate method
pDialog = new ProgressDialog(CreateProject.this);
pDialog.setMessage("Lädt...");
pDialog.setIndeterminate(true);
// execute this when the downloader must be fired
DownloadFile downloadFile = new DownloadFile();
downloadFile.execute();
작동하지 않습니다, 내가 추가 한 짧은 튜토리얼을 읽을 수 있습니다 내 주요 게시물에. progressbar는 보여 주지만'Thread.sleep'에서 2 초를 기다리지 않습니다. 제 코드에 실수가 있습니까? –
Thread.sleep 대신 Timer를 사용하십시오. –
doInBackground()에서 Timer를 사용하는 것이 맞습니까? onProgressUpdate()에서 코드가 필요하지 않습니까? –