如何找回在AsyncTask的任务完成情况 [英] How to get back the task completion status in AsyncTask

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

问题描述

这是关系到我的previous交<一href="http://stackoverflow.com/questions/5079335/problem-with-downloading-multiple-files-using-asynctask">Problem与下载使用AsyncTask的多个文件

我试图下载两个视频文件,还可以在此过程中表现出ProgressDialog。对于这个我使用AsyncTask的。我想第一次下载完成,释放内存,然后开始第二个下载。我写了下面code来实现这一点,但它似乎第二个下载从来就开始了。

  startDownload(){
   DownloadFileAsync D1 =新DownloadFileAsync();
   d1.execute(将videoPath +文件名[0],文件名[0]);

   如果(d1.getStatus()== AsyncTask.Status.FINISHED){
     D1 =无效;
     DownloadFileAsync D2 =新DownloadFileAsync();
     d2.execute(将videoPath +文件名[1],文件名[1]);
   }
}
 

有没有办法,我能找回我的第一个任务和放大器的完成情况;然后开始第二个?

以下是我DownloadFileAsync类的code:

 类DownloadFileAsync扩展的AsyncTask&LT;字符串,字符串,字符串&GT; {

    @覆盖
    在preExecute保护无效(){
        super.on preExecute();
        的ShowDialog(DIALOG_DOWNLOAD_PROGRESS);
    }

    @覆盖
    保护字符串doInBackground(字符串... aurl){
        诠释计数;

        尝试 {
            网址URL =新的URL(aurl [0]);
            URLConnection的体conexion = url.openConnection();
            conexion.connect();

            文件根= android.os.Environment.getExternalStorageDirectory();

            INT lenghtOfFile = conexion.getContentLength();
            Log.d(ANDRO_ASYNC的Lenght文件:+ lenghtOfFile);

            的InputStream输入=新的BufferedInputStream(url.openStream());
            的OutputStream输出=新的FileOutputStream(root.getAbsolutePath()+/影片/+ aurl [1]);

            字节的数据[] =新的字节[1024];

            总长= 0;

            而((计数= input.read(数据))!=  -  1){
                共有+ =计数;
                publishProgress(+(int)的((总* 100)/ lenghtOfFile));
                output.write(数据,0,计数);
            }
            output.flush();
            output.close();
            input.close();

        }赶上(例外五){}
        返回null;

    }
    保护无效onProgressUpdate(字符串...进度){
         Log.d(ANDRO_ASYNC,进步[0]);
         mProgressDialog.setProgress(的Integer.parseInt(进展[0]));
    }

    @覆盖
    保护无效onPostExecute(字符串使用){
        dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
        tv.append(\ñ\ n文件下载完成!);
        sendBroadcast(新意图(Intent.ACTION_MEDIA_MOUNTED,Uri.parse(文件://+ Environment.getExternalStorageDirectory())));
    }
}
 

解决方案

由于DKIT Android的建议,你可以开始从onPostExecute第二次下载,但前提是,例如下载1为空

  @覆盖
保护无效onPostExecute(字符串使用)
{

   如果(D2 == NULL)
   {
       D2 =新DownloadFileAsync();
       d2.execute(将videoPath +文件名[1],文件名[1]);
   }
}
 

如果你需要开始更多的下载量,只写方法,您的AsyncTask之外,这将检查哪些下载下一步应该开始了。

This is related to my previous post Problem with downloading multiple files using 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 ?

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("\n\nFile Download Completed!");
        sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));            
    }
}

解决方案

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.

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

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