2015-11-12 5 views
0

У меня есть класс в моей деятельности, который загружает mp3-файл и затем возвращает некоторые данные. Перед завершением загрузки выполняется проверка состояния. Процесс загрузки выполняется в фоновом режиме.проверить состояние после загрузки

Я хочу выполнить проверку состояния после завершения загрузки. Не могли бы вы упомянуть об этом?

update Он отлично работает, но после его завершения загружает результатFromDownload = getFromSdcard(); выполняет. , но после того, что я хочу, чтобы вернуть это заявление выполнить:

return resultFromDownload; 

, но ничего не происходит после того, как onPostExecute.

private String downloadSomeFiles(String filename) { 

DownloadImage downloadimage = new DownloadImage(); 
downloadimage.execute(filename); 
    **return resultFromDownload;** 
} 

это AsyncTask:

private class DownloadImage extends AsyncTask<String, Integer, Boolean> { 

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

    // This is run in a background thread 
    @Override 
    protected Boolean doInBackground(String... params) { 
     // get the string from params, which is an array 

     Boolean resul = true; 
     try{ 
      DownloadRequest downloadRequestLrc = new DownloadRequest() 
        .downloadPath("http://192.168.1.108/music+".mp3) 
        .filepath(G.DIR_APP + "/" + music+ ".mp3") 
        .simulate(true) 
        .download(); 
      downloadRequests =downloadRequestLrc; 

     } catch (Exception e){ 
      resul=false; 
     } 

     return resul; 
    } 

    // This is called from background thread but runs in UI 
    @Override 
    protected void onProgressUpdate(Integer... values) { 
     super.onProgressUpdate(values); 

     // Do things like update the progress bar 
    } 

    // This runs in UI when background thread finishes 
    @Override 
    protected void onPostExecute(Boolean result) { 
     if(result){ 
      // Log.i("resulyyyy : ",result); 

      **resultFromDownload= getFromSdcard();** 

      Log.i("result1 : ",resultFromDownload); 

     } else { 
      resultFromDownload=""; 
      Log.i("result2 : ",resultFromDownload); 

     } 


     // Do things like hide the progress bar or change a TextView 
    } 
} 
+1

Вы можете поместить его в 'AsyncTask' и делать то, что вам нужно после того, как она заканчивается в' onPostExecute() ' – codeMagic

ответ

0

Вы можете создать AsyncTask класс, чтобы сделать это, как @codeMagic сказал.

Примером может быть:

private class DownloadImage extends AsyncTask<String, Integer, Boolean> { 

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

    // This is run in a background thread 
    @Override 
    protected Boolean doInBackground(String... params) { 
     // get the string from params, which is an array 

     Boolean resul = true; 
     try{ 
      DownloadRequest downloadRequestLrc = new DownloadRequest() 
        .downloadPath("http://192.168.1.108/music.mp3") 
        .filepath(G.DIR_APP + "/" + params[0] + ".mp3") 
        .simulate(true) 
        .download(); 
      downloadRequests =downloadRequestLrc; 

     } 
     catch (Exception e){ 
      resul=false; 
     } 

     return resul; 
    } 

    // This is called from background thread but runs in UI 
    @Override 
    protected void onProgressUpdate(Integer... values) { 
     super.onProgressUpdate(values); 

     // Do things like update the progress bar 
    } 

    // This runs in UI when background thread finishes 
    @Override 
    protected void onPostExecute(Boolean result) { 
     if(result){ 
      //All allright 
     } 
     else{ 
      //Problem! 
     } 

     // Do things like hide the progress bar or change a TextView 
    } 
} 

И вызов будет:

DownloadImage downloadimage = new DownloadImage(); 
           downloadimage.execute(filename)); 
+0

Что такое результат onPostExecute? – coder3641

+0

Посмотрите на это изображение, это ясно объяснено [AsyncTask] (http://i.stack.imgur.com/I23KW.png) –

+0

Я обновляю свой вопрос. – coder3641

Смежные вопросы