如何在AsyncTask中取回任务完成状态 [英] How to get back the task completion status in AsyncTask

查看:15
本文介绍了如何在AsyncTask中取回任务完成状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与我之前的帖子有关 使用 AsyncTask 下载多个文件的问题

This is related to my previous post Problem with downloading multiple files using AsyncTask

我正在尝试下载两个视频文件并在此过程中显示一个 ProgressDialog.为此,我正在使用 AsyncTask.我希望第一次下载完成,释放内存然后开始第二次下载.我编写了以下代码来实现这一点,但似乎第二次下载从未开始.

I'm trying to download two video files and also show a ProgressDialog during the process. For this I'm using AsyncTask. I want the 1st download to complete, free up memory then start the 2nd download. I wrote the following code to achieve this, but it seems the 2nd download never begins.

startDownload() {
   DownloadFileAsync d1 = new DownloadFileAsync();
   d1.execute(videoPath+fileNames[0],fileNames[0]);

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

有什么办法可以让我找回第一个任务的完成状态&然后开始第二个?

Is there a way that I can get back the completion status of my 1st task & then start the 2nd ?

以下是我的 DownloadFileAsync 类的代码:

The following is the code of my DownloadFileAsync class:

class DownloadFileAsync extends AsyncTask<String, String, String> {

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

    @Override
    protected String doInBackground(String... aurl) {
        int count;

        try {
            URL url = new URL(aurl[0]);
            URLConnection conexion = url.openConnection();
            conexion.connect();

            File root = android.os.Environment.getExternalStorageDirectory();

            int lenghtOfFile = conexion.getContentLength();
            Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);

            InputStream input = new BufferedInputStream(url.openStream());
            OutputStream output = new FileOutputStream(root.getAbsolutePath() + "/videos/" + aurl[1]);

            byte data[] = new byte[1024];

            long total = 0;

            while ((count = input.read(data)) != -1) {
                total += count;
                publishProgress(""+(int)((total*100)/lenghtOfFile));
                output.write(data, 0, count);                    
            }
            output.flush();
            output.close();                
            input.close();

        } catch (Exception e) {}
        return null;

    }
    protected void onProgressUpdate(String... progress) {
         Log.d("ANDRO_ASYNC",progress[0]);
         mProgressDialog.setProgress(Integer.parseInt(progress[0]));
    }

    @Override
    protected void onPostExecute(String unused) {
        dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
        tv.append("

File Download Completed!");
        sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));            
    }
}

推荐答案

正如 DKIT Android 所建议的,您可以从 onPostExecute 开始第二次下载,但前提是例如 download2 为 null

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]);    
   }    
}

如果您需要启动更多下载,只需在 asynctask 之外编写该方法,它将检查接下来应该启动哪个下载.

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

这篇关于如何在AsyncTask中取回任务完成状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆