Android

时间:2017-07-19 10:18:44

标签: asynchronous task

尝试一次为多个文件调用async-Task

 case Type1:
            srcCopydir = Dir1
            copyfilename = file1;               
            copyFilesAsyncTask.execute(srcCopydir, copyfilename);
             break;  
         case Type2:              
            srcCopydir = Dir2
            copyfilename = file2;
            copyFilesAsyncTask.execute(srcCopydir, copyfilename);
            break;

        case Type3:
            srcCopydir = Dir3;
            copyfilename = file3
            copyFilesAsyncTask.execute(srcCopydir, copyfilename);
            break;

         case Type4:
            srcCopydir = Dir3;
            copyfilename = file3
            copyFilesAsyncTask.execute(srcCopydir, copyfilename);
            break;

所有这些案例的Asysnc任务一个接一个地开始

“doInBackground”正确运行并正确返回结果,但有时在执行一个任务的“onPostExecute”之前执行asynctask时,“doInBackground”会启动另一个任务

因此onPostexecute返回其他任务的结果(例如Type2:返回对应于Type 3和Type3的异步任务输出:异步任务返回Type3预期的输出)

2 个答案:

答案 0 :(得分:0)

尝试在每种情况下创建AsyncTask的新实例。像这样:

case Type1:
    srcCopydir = Dir1
    copyfilename = file1;               
    new YourAsyncTask().execute(srcCopydir, copyfilename);
    break;  
case Type2:              
    srcCopydir = Dir2
    copyfilename = file2;
    new YourAsyncTask().execute(srcCopydir, copyfilename);
    break;
case Type3:
    srcCopydir = Dir3;
    copyfilename = file3
    new YourAsyncTask().execute(srcCopydir, copyfilename);
    break;
case Type4:
    srcCopydir = Dir3;
    copyfilename = file3
    new YourAsyncTask().execute(srcCopydir, copyfilename);
    break;

答案 1 :(得分:0)

 private class Task extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
      //preExecute
    }
    @Override
    protected Void doInBackground(Void... voids) {
        //Execution Code
        return null;
    }
    @Override
    protected void onPostExecute(Void result) {
      if(yes){
        //Call new Task
        new NewTask().execute();
      }
    }
}