0

我创建了一个代码来下载显示进度条进度的多个文件。这完美地工作,但现在我想将它部署到外部项目(库本身)并且我遇到了问题。

我的意图是调用“下载”,当下载完成或返回 0 或 1。但是异步的方法在完成之前返回值。我试图用“CountdownLatch”控制它但没有成功......知道吗?

以下代码有效,问题是“下载”方法返回值而不等待它完成其余代码。

主.java

if(MyLib.download(Main.this, url, nameFiles, "internal", "folderExtra") == 1){
            System.out.println("SUCCESS DOWNLOAD!");
}else{
            System.out.println("error download");
}

MyLib.java

/** DOWNLOADER WITH PROCESSDIALOG **/
    ProgressDialog pDialog;
    Context context;
    String urlName; 
    String[] filesToDownload;
    int downloadOK = -1;
    int currentFile = -1;
    int totalFiles = 0;
    String typeStorage = "internal";
    String folder = "";
    CountDownLatch controlLatch;
    public int download(Context ctx, String urlName, String[] filesToDownload, String typeStorage, String extraFolder ){

            System.out.println("estoy en download");
            this.context = ctx;
            this.urlName = urlName;
            this.filesToDownload = filesToDownload;
            this.totalFiles = filesToDownload.length;
            this.typeStorage = typeStorage;

            /** Almacenamiento de la descarga - Interno o externo **/
            if (typeStorage.equalsIgnoreCase("internal")) {
                System.out.println("internal");
                this.folder = context.getFilesDir().toString() + "/";
            } else if (typeStorage.equalsIgnoreCase("external")) {
                System.out.println("external");
            }

            /** COMPLETEMENTO DIRECTORIO/S OPCIONAL **/
            if (extraFolder != null && extraFolder != "") {
                folder += extraFolder;
            }

            System.out.println("FOLDER: " + folder);
            File directoryFile = new File(folder);
            if (!directoryFile.isDirectory()) {
                if (!directoryFile.mkdir()) {
                    Toast.makeText(context, "No se puede crear el directorio o no existe", Toast.LENGTH_LONG).show();
                }
            }
            pDialog = new ProgressDialog(context);
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.setMax(100);
            pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            pDialog.setTitle("Descargando recursos...");

            // controlLatch = new CountDownLatch(1);

            startDownload();
            /* 
            try {
                System.out.println("STOP");
                controlLatch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            */          
        return downloadOK;
    }
private void startDownload() {
    currentFile++;
    System.out.println("startDownload!");
    if (currentFile < totalFiles) {
        pDialog.setMessage("Descargando " + (currentFile + 1) + " de " + totalFiles + "\n\n(..." + filesToDownload[currentFile] + ")");
        System.out.println("startDownload currentFile +[" + currentFile + "] totalFiles [" + totalFiles + "]");
        System.out.println("file: " + filesToDownload[currentFile].toString());
        new DownloaderFile().execute(filesToDownload[currentFile]);
    }
}

private class DownloaderFile extends AsyncTask<String, Integer, String> {

    @Override
    protected String doInBackground(String... params) {
        try {
            File checkFile = new File(folder + params[0]);
            if(!checkFile.exists()){
                URL urlFinal = new URL(urlName+params[0]);
                URLConnection connection = urlFinal.openConnection();
                connection.connect();
                int fileLength = connection.getContentLength();

                InputStream input = new BufferedInputStream(urlFinal.openStream());
                OutputStream output = new FileOutputStream(folder + params[0]);

                byte data[] = new byte[1024];
                long total = 0;
                int count;
                while ((count = input.read(data)) != -1) {
                    total += count;
                    publishProgress((int) (total * 100 / fileLength));
                    output.write(data, 0, count);
                }
                output.flush();
                output.close();
                input.close();
            } else {
                System.out.println("The file " + filesToDownload[currentFile] + " is downloaded"  );
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
        return "ok";

    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        System.out.println("-1");
        File checkFile = new File(folder + filesToDownload[currentFile]);
        if(!checkFile.exists()){
            System.out.println("pDialogShow");
            pDialog.show();
        }
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {
        super.onProgressUpdate(progress);
        pDialog.setProgress(progress[0]);
    }

    @Override
    protected void onPostExecute(String res) {
        System.out.println("10");
        if (currentFile == (totalFiles - 1)) {
            System.out.println("FINISH!!!!!!!!!!!!!!");
            currentFile = 0;
            pDialog.dismiss();
            downloadOK = 1;
            //controlLatch.countDown();

        } else {
            if (res.equals("ok")) {
                startDownload();
            } else {
                System.out.println("error download");
                downloadOK = 0;
            }
        }
    }
}
4

1 回答 1

1

听起来你想要回调。

您的DownloaderFile类可以采用一个侦听器接口,该接口由包含该DownloaderFile对象的任何对象实现。调用里面的那个监听器onPostExecute()

它可能看起来像这样:

public class MyActivity extends Activity implements OnDownloadCompleteListener {
    private void startDownload() {
        //inside whatever method        
        Downloader downloader = new Downloader(this); //'this' implements OnDownloadCompleteListener
        downloader.go();
    }

    @Override 
    public void onDownloadFinished(boolean success) {
        //implementation 

    }
}

内部Downloader

//...
@Override
protected void onPostExecute(String res) {
    onDownloadCompleteListener.onDownloadComplete(true);
}

OnDownloadCompleteListener 接口:

public interface OnDownloadCompleteListener {
    public void onDownloadComplete(boolean success);
}
于 2013-08-08T19:19:06.267 回答