0

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(); 

답변

1

당신이 할 수있는 귀하의 요구 사항을 asy로 채우십시오. nctask onPreExecute() 진행률 대화 상자를 표시하고 doInBackground()에서 프로세스를 수행하고 onPostExecture는 대화 상자를 닫고 결과를 표시합니다.

+0

작동하지 않습니다, 내가 추가 한 짧은 튜토리얼을 읽을 수 있습니다 내 주요 게시물에. progressbar는 보여 주지만'Thread.sleep'에서 2 초를 기다리지 않습니다. 제 코드에 실수가 있습니까? –

+0

Thread.sleep 대신 Timer를 사용하십시오. –

+0

doInBackground()에서 Timer를 사용하는 것이 맞습니까? onProgressUpdate()에서 코드가 필요하지 않습니까? –

2

나는 이걸 AsynsTask 클래스로 구현해야한다고 생각합니다. 명확하고 빠르고 쉽습니다. 당신은 약 AsyncTaskhere

+0

내가 이미 지정한 내용입니다. –

0

그냥 서브 클래스 onPostExecute에 super.onPostExecute() 대신 super.onPreExecute()를 호출하거나 내가 AsyncTask를 구현 잘