2012-11-07 53 views
1

我想開發文件下載器應用程序。該文件下載列表。當我將文件添加到下載程序列表,然後定期更新其進度。 我關閉了所有的活動,然後我運行應用程序,然後我的應用程序列表中的所有當前下載連續進度條讓下載器在後臺下載文件並上傳進度條

我該怎麼做?

我使用這個異步任務下載文件

class DownloadFileFromURL extends AsyncTask<String, String, String> { 
      @Override 
      protected void onPreExecute() { 
       super.onPreExecute(); 
      } 

      @Override 
      protected String doInBackground(String... f_url) { 
       int count; 
       try { 
        URL url = new URL(f_url[0]); 
        URLConnection conection = url.openConnection(); 
        conection.connect(); 
        // getting file length 
        lenghtOfFile = conection.getContentLength(); 

        // input stream to read file - with 8k buffer 
        InputStream input = new BufferedInputStream(
          url.openStream(), 8192); 
        OutputStream output = null; 

        if (space_avaliable) { 
         File dir = new File(
           Utill.saveFile(CustomGroupTabActivity.mTabHost 
             .getContext()) + "/tocaonline"); 
         if (!dir.exists()) { 
          dir.mkdirs(); 
         } 
         // Output stream to write file 
         output = new FileOutputStream(
           Utill.saveFile(CustomGroupTabActivity.mTabHost 
             .getContext()) 
             + "/tocaonline/" 
             + "test.mp3"); 

         byte data[] = new byte[1024]; 

         long total = 0; 

         while ((count = input.read(data)) != -1) { 
          total += count; 
          // publishing the progress.... 
          // After this onProgressUpdate will be called 
          publishProgress("" 
            + (int) ((total * 100)/lenghtOfFile)); 

          // writing data to file 
          output.write(data, 0, count); 
         } 

         // flushing output 
         output.flush(); 
        } 

        // closing streams 
        output.close(); 
        input.close(); 

       } catch (Exception e) { 
        Log.e("Error: ", e.getMessage()); 
       } 

       return null; 
      } 

      protected void onProgressUpdate(String... progress) { 
       // setting progress percentage 
       p_bar.setProgress(Integer.parseInt(progress[0])); 
      } 

      @SuppressWarnings("unchecked") 
      @Override 
      protected void onPostExecute(String file_url) { 

      } 
     } 

當我上添加點擊下載按鈕,然後我加入到線性佈局動態視圖。

LinearLayout lytParent = (LinearLayout) findViewById(R.id.boxset_parentview); 
     LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     inflater = LayoutInflater.from(CustomGroupTabActivity.mTabHost 
       .getContext()); 

     View convertview = inflater.inflate(R.layout.raw_set_detail, null); 

final ProgressBar p_bar = (ProgressBar) convertview 
       .findViewById(R.id.rawsetdetails_sync_progress); 

lytParent.addView(convertview); 

當我的活動處於活動狀態時,它運行良好。但是當我關閉所有活動並再次運行此活動時,我想通過活動下載來顯示實際的下載進度。

+0

發表你試過的東西。 – slezadav

+0

請在我編輯的問題 –

回答

0

我假設你使用的是ListView。
在這種情況下,您必須在getView()中添加代碼,例如:
如果下載正在運行:顯示progressBar
否則:隱藏progressBar。

這是因爲如果可能的話,Android會重新使用視圖,所以也許視圖仍然是Visibile。

+0

Thanx中找到答案,但是當我關閉所有活動並再次運行相同的活動,然後我想顯示以前的下載文件,更新下載進度。我如何恢復所有的數據? –

3

這如何可以實現它:

1)下載後運行一個後臺服務。這link爲您提供基本的服務。它會使用線程池進行多個活動下載。它將以區塊形式讀取數據並將塊讀取事件發送到註冊監聽器,以便活動可以更新其UI。

2)每當你想下載,發送請求到服務與項目名稱,項目ID和http鏈接。服務會爲每次下載保留這三樣東西,以及下載的總大小和下載的字節數。

3)現在,無論何時啓動活動(讓電話是下載管理器屏幕),然後要求服務提供當前活動和排隊下載。服務將提供下載項目列表(項目名稱,項目ID,項目http鏈接,項目總大小和下載的字節數),您可以使用此項目使用ListView創建視圖。您可以使用項目總大小和下載字節數來創建進度欄。

4)用服務註冊塊讀事件。因此,服務會在每個塊讀取下載請求後通知活動,其中包含項目名稱,項目ID,項目http鏈接,項目總大小和下載的字節數。使用此信息更新活動中的項目列表,並通知適配器數據已更改爲更新列表視圖。

5)當活動關閉時,未註冊事件偵聽器。