2012-07-04 160 views
1

我有一個嘗試和趕上「doInBackground」,我不會運行代碼運行onPostExecute如果應用程序在「doInBackground」執行catch方法。代碼如下:asynctask取消onpostexecute

public class DownloadFileAsync extends AsyncTask<String, Integer, Void> { 
     @Override 
     protected void onPreExecute(){ 
      super.onPreExecute(); 
    } 

    @Override 
    protected Void doInBackground(String... aurl) { 
     try{ 
      //some code, jump to onPostexecute after complete 
     } 
     catch{ 
      //some code, don't do onPostExecute after complete 
     } 
     return null; 

     } 

    @Override 
    protected void onProgressUpdate(Integer... progress) {   
    //some code 
    } 

    @Override 
    protected void onPostExecute(Void unused) { 
    //some code 
    } 
} 

回答

3

返回布爾數據類型從doInBackground()到您的AsyncTask的onPostExecute()。並檢查它的價值你要成功做什麼,

@Override 
    protected boolean doInBackground(String... aurl) { 
     try{ 
      //some code, jump to onPostexecute after complete 

     return true; 
     } 
     catch{ 
      //some code, don't do onPostExecute after complete 

     return false; 
     } 
     } 

@Override 
    protected void onPostExecute(boolean flag) { 
    if(flag) 
    { 
     //some code 
    } 
    } 
+0

THX,它爲我工作:) – melvintcs

1

你可以有一個布爾變量在AsyncTask並檢查該變量。例如:

public class DownloadFileAsync extends AsyncTask<String, Integer, Void> { 

private boolean run_do_in_background = true; 

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

@Override 
protected Void doInBackground(String... aurl) { 
    try{ 
     //some code, jump to onPostexecute after complete 
    } 
    catch{ 
     //some code, don't do onPostExecute after complete 
     run_do_in_background = false; 
    } 
    return null; 

    } 

@Override 
protected void onProgressUpdate(Integer... progress) {  

//some code 
} 

@Override 
protected void onPostExecute(Void unused) { 
if(run_do_in_background) { //only if this is true will excecute the code in doInBackground 
//some code 
} 
} 
} 
1

使用Pair這樣你就可以返回兩個結果

例如,將與該類型要任何回報:

public class DownloadFileAsync extends AsyncTask<String, Integer, Pair<Exception, ANYTHING> > {  
    @Override 
    protected Pair<Exception, ANYTHING> doInBackground(String... aurl) { 
     try{ 
      return new Pair<Exception, ANYTHING>(null, /* anything can go here */); 

     } 
     catch (Exception e) { 
      return new Pair<Exception, ANYTHING>(e, null); 
     }  
    } 

    @Override 
    protected void onPostExecute(Pair<Exception, ANYTHING> result) { 
     // Evaluate the exception here 
     if(result.first != null) { 
      // an eror occured, do something about it 
     } 
    } 
} 

這種方式可以讓你處理一個在onPostExecute中發生異常,並且如果一切正常,從result.second得到結果。

6

您可以嘗試在doInBackground()中使用cancel(true)。試試下面的代碼:

protected Void doInBackground(String... aurl) { 
    try{ 
     //some code, jump to onPostexecute after complete 
    } 
    catch{ 
     cancel(true); 
    } 
    return null; 

    } 

的正式照會上cancel()

調用此方法將導致onCancelled(對象)doInBackground之後被調用 在UI線程(對象[])的回報。調用這個 方法可以保證不會調用onPostExecute(Object)。在 調用此方法之後,應該儘可能早地檢查由doInBackground(Object [])定期檢查由 isCancelled()返回的值以儘快完成 任務。

檢查此鏈接瞭解更多詳情:

http://developer.android.com/reference/android/os/AsyncTask.html#cancel(boolean

+0

也許因爲它最好的答案在文檔中指出:調用此方法保證onPostExecute(對象)從不被調用。來源:http://developer.android.com/reference/android/os/AsyncTask.html#cancel%28boolean%29 –

+0

thx爲答案,簡短,非常理解。但它不適用於我:(不管怎樣,起來就像你:) – melvintcs