如何防止方法在完成其功能之前返回默认值?

时间:2013-08-09 10:53:55

标签: android callback android-asynctask android-library

我有两个项目:App Project和自己的库。 我的项目有这段代码:

main.java(MyProject)

...
// 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;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

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

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

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

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