DownloadManagerを使用してURLからxmlファイルをダウンロードします。次に、スレッドを使用して2秒間待機し、ファイルをSDカードに保存します。
ここに示すような活動サークルが欲しいのですが。これを実現する最も簡単な方法は何ですか?実装する必要があり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();
}
更新 これが私の実装された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();