2015-09-12 51 views
0

我使用AsyncTask將一些文件下載到我的應用程序,當我刷新它們時我希望可用文件的列表自行更新。從我在線閱讀的部分下載部分將在「doInBackground」方法中完成,之後爲了更新視圖,我使用了「onPostExecute」方法。當我這樣做時,視圖不會自行更新,我必須更改活動,然後返回以更新它,這是我用來更新視圖的方法,但似乎它在所有文件已完成下載,因此它們不會顯示可用文件。Android異步任務方法執行順序

請糾正我,如果我錯了,但不是在doInBackground完全完成後調用onPostExecute?如果沒有,我可以做些什麼,以便在文件完成下載之前不會調用onPostExecute?

謝謝!

這裏是我使用的代碼:

private class Documents_Task extends AsyncTask<String, String, Boolean> { 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
     } 

     @Override 
     protected Boolean doInBackground(String... params) { 

      try { 

       // Downloading the documents and savign them 
       return true; 

      } catch (Exception e) { 
       return false; 
      } 
     } 

     @Override 
     protected void onPostExecute(Boolean success) { 

      if (success) { 
       // Displaying the list of available files 
      } 
     } 
    } 

編輯:He're用於下載和保存文件

String result = HttpManager.getData(params[0]); 
       // Getting the names of the files I already have on my mobile 
       File folder = (Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS + "/EHTProfs/Documents")); 
       File[] listOfFiles = folder.listFiles(); 

       //String array, each element will correspond to a name of a file on my mobile 
       String[] names = new String[listOfFiles.length]; 

       //Filling my array 
       if (listOfFiles.length > 0) 
        for (int i = 0; i < listOfFiles.length; i++) { 
         if (listOfFiles[i].isFile()) 
          names[i] = listOfFiles[i].getName(); 
        } 

       Context c = Documents.this; 

       // Taking null out of the string result containing the names of the files on my server 
       String sub_result = result.substring(1, result.length() - 6); 

       // Comparing each file name on the server with those on my mobile, 
       // if it exists do nothing, if not download it 
       StringTokenizer st = new StringTokenizer(sub_result, "\",\""); 
       Boolean exists; 

       while (st.hasMoreTokens()) { 
        exists = false; 
        String fileName = st.nextToken(); 
        if (names.length > 0) 
         for (int i = 0; i < names.length; i++) { 
          if (names[i].equals(fileName)) { 
           i = names.length; 
           exists = true; 
          } 
         } 
        if (!exists) { 
         downloadDocument(fileName); 
        } 
       } 

的代碼下面是下載一個文件

方法
public void downloadDocument(String fileName) { 

     DownloadManager mManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); 
     Uri downloadUri = Uri.parse("http://" + IP_ADDRESS_PORT + "/EhtprofsWEB/ehtprofs/document/" + fileName); 
     DownloadManager.Request request = new DownloadManager.Request(
       downloadUri) 
       .setAllowedOverRoaming(false) 
       .setTitle(fileName) 
       .setDestinationInExternalPublicDir(
         Environment.DIRECTORY_DOWNLOADS + "/EHTProfs/Documents", fileName); 
     mManager.enqueue(request); 
    } 
+0

您可能需要做出詳細的下載/保存代碼。 – headuck

+0

@headuck我添加了代碼 –

回答

0

我想你在實際下載完成之前傳遞了doInBackground的結果。您可以在下載的成功中寫入回報。這將解決你的問題。

+0

應該在哪裏添加onSuccess? –

0

您在下載代碼中調用的DownloadManager正在爲您處理異步下載,並立即返回下載請求。因此您的doInBackground()呼叫在下載完成之前快速返回。

要達到您想要的效果,您可能根本不需要AsyncTask。相反,您可以設置一個廣播接收器,以便在下載完成時收聽DownloadManager廣播。請參閱以下答案和代碼示例,以供參考。

DownloadManager.ACTION_DOWNLOAD_COMPLETE broadcast receiver receiving same download id more than once with different download statuses in Android