1

我尝试在“doInBackground”中捕获,如果应用程序在“doInBackground”中执行 catch 方法,我不希望代码运行 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
    }
}
4

4 回答 4

6

您可以尝试使用cancel(true)in doInBackground()。试试下面的代码:

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

    }

关于官方说明cancel()

调用此方法将导致在 doInBackground(Object[]) 返回后在 UI 线程上调用 onCancelled(Object)。调用此方法可确保永远不会调用 onPostExecute(Object)。调用此方法后,应定期从 doInBackground(Object[]) 中检查 isCancelled() 返回的值,以尽早完成任务。

检查此链接以获取更多详细信息:

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

于 2012-07-04T07:08:28.827 回答
3

从您的AsyncTask 中返回布尔数据类型。并检查它的价值你想为成功做什么,doInBackground()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
     }
    }
于 2012-07-04T07:00:03.887 回答
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
}
}
}
于 2012-07-04T07:01:02.427 回答
1

使用Pair可以返回两个结果

例如,将 ANYTHING 替换为您要返回的类型:

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 获取结果。

于 2012-07-04T07:07:31.477 回答