How to get back the task completion status in AsyncTask

倖福魔咒の 提交于 2019-12-01 22:54:12

As DKIT Android suggested, you could start the second download from onPostExecute, but only if for example download2 is null

@Override
protected void onPostExecute(String unused) 
{

   if (d2 == null)
   {
       d2 = new DownloadFileAsync();
       d2.execute(videoPath+fileNames[1],fileNames[1]);    
   }    
}

If you need to start more downloads, just write the method outside of your asynctask, which will check which download should be started next.

Start your second download from within your first onPostExecute-method.

protected void onPostExecute(String unused) {
        dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
        tv.append("\n\nFile Download Completed!");
        sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));            

        // Here you start your new AsyncTask...
    }

This code:

if(d1.getStatus()==AsyncTask.Status.FINISHED) {
 d1 = null;
 DownloadFileAsync d2 = new DownloadFileAsync();
 d2.execute(videoPath+fileNames[1],fileNames[1]);

}

...will execute once, and immediately after the first AsyncTask is executed, thus it will always be false. If you want to go that route, you need to do it in a while loop - which kind of defeats the point of making the task Async in the first place.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!