0

我有 2 个项目:应用程序项目和自己的库。我的项目有这个代码:

main.java (我的项目)

...
// call method Lib.download()
String nameFiles[] = {"do.mp3","re.mp3","mi.mp3","billiejean.mp3"};
String url = "http://myweb.net/getfiles/";
if( Lib.download(Main.this, url, nameFiles, "internal", "folderExtra" ) == 1){
    System.out.println("finish");
} else {
    System.out.println("error download");
}

问题是 Lib.download 返回默认值而不等待它完成其余代码。 我正在尝试使用 Semaphore / CountDownLatch / whiles 来控制它,但它不起作用,而且我正在尝试实现回调但没有成功,因为“下载”类必须不在我的项目中。有什么帮助吗?

下载已正确完成,但下载方法在完成下载之前返回值......我的意图是从任何活动中调用“下载”方法,这会启动对话过程,并且我的其余活动代码在此之前不会运行方法“下载”返回一个值。

Lib.java (MyLib)

/** 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 ){
            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")) {
            }

            /** EXTRA OPTIONALL **/
            if (extraFolder != null && extraFolder != "") {
                folder += extraFolder;
            }

            File directoryFile = new File(folder);
            if (!directoryFile.isDirectory()) {
                if (!directoryFile.mkdir()) {
                    Toast.makeText(context, "problems create directory", 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

0

.execute()一旦你被调用,AsyncTasks 将在单独的线程中运行。您的调用线路startDownload()将永远不会等待 AsyncTask 返回任何值,您可以做的是:

  1. 在您的主要活动上实现一些回调接口,以便您的 onPostExecute 方法能够在操作完成后通知您的主要活动。

  2. 由于您是开发此库的人,请考虑将 AsyncTask 移至您的主要活动,并在库中保留非常简单的下载功能(您的 doInBackground 方法中的内容)。

  3. 使用 LocalBroadcastManager 从您的图书馆发送广播,在您的 main.java 上收听并做出反应

于 2013-08-09T12:05:48.847 回答