我尝试在“ 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
    }
}

最佳答案

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
     }
    }

关于android - asynctask取消onpostexecute,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11323906/

10-11 20:23