0

I'm using AsyncTask to download some files to my app, when I refresh them I'd like the list of available files to be updated on its own. From what I've read online the downloading part is to be done in the "doInBackground" method, afterwards in order to update the view I use the method "onPostExecute". When I do this the view isn't updated on its own, I have to change activities then go back for it to be updated, the method I'm using to update the view works, but it seems that it's being called before all the files are finished downloading, so they aren't displayed with the available files.

Please correct me if I'm wrong, but isn't onPostExecute called after doInBackground is completely finished ? If not, what can I do so that the onPostExecute isn't called until the files are finished being downloaded ?

Thanks !

Here is the code I'm using:

private class Documents_Task extends AsyncTask<String, String, Boolean> {

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

        @Override
        protected Boolean doInBackground(String... params) {

            try {

                // Downloading the documents and savign them
                return true;

            } catch (Exception e) {
                return false;
            }
        }

        @Override
        protected void onPostExecute(Boolean success) {

            if (success) {
                // Displaying the list of available files
            }
        }
    }

EDIT: He're the code for downloading and saving the files

String result = HttpManager.getData(params[0]);
                // Getting the names of the files I already have on my mobile
                File folder = (Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS + "/EHTProfs/Documents"));
                File[] listOfFiles = folder.listFiles();

                //String array, each element will correspond to a name of a file on my mobile
                String[] names = new String[listOfFiles.length];

                //Filling my array
                if (listOfFiles.length > 0)
                    for (int i = 0; i < listOfFiles.length; i++) {
                        if (listOfFiles[i].isFile())
                            names[i] = listOfFiles[i].getName();
                    }

                Context c = Documents.this;

                // Taking null out of the string result containing the names of the files on my server
                String sub_result = result.substring(1, result.length() - 6);

                // Comparing each file name on the server with those on my mobile,
                // if it exists do nothing, if not download it
                StringTokenizer st = new StringTokenizer(sub_result, "\",\"");
                Boolean exists;

                while (st.hasMoreTokens()) {
                    exists = false;
                    String fileName = st.nextToken();
                    if (names.length > 0)
                        for (int i = 0; i < names.length; i++) {
                            if (names[i].equals(fileName)) {
                                i = names.length;
                                exists = true;
                            }
                        }
                    if (!exists) {
                        downloadDocument(fileName);
                    }
                }

Here's the method for downloading one file

public void downloadDocument(String fileName) {

        DownloadManager mManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
        Uri downloadUri = Uri.parse("http://" + IP_ADDRESS_PORT + "/EhtprofsWEB/ehtprofs/document/" + fileName);
        DownloadManager.Request request = new DownloadManager.Request(
                downloadUri)
                .setAllowedOverRoaming(false)
                .setTitle(fileName)
                .setDestinationInExternalPublicDir(
                        Environment.DIRECTORY_DOWNLOADS + "/EHTProfs/Documents", fileName);
        mManager.enqueue(request);
    }
Husayn Hakeem
  • 4,184
  • 1
  • 16
  • 31

2 Answers2

0

I think you are passing the result of doInBackground before actual download is finished. You can write return in onSuccess of download. That will solve your problem.

Punit Vajpeyi
  • 196
  • 11
0

The DownloadManager you called in the download code is handling the async download for you, and the download request returns immediately. Thus your doInBackground() call returns quickly before download is completed.

To achieve what you want, you may not need AsyncTask at all. Instead you may set up a broadcast receiver to listen to the DownloadManager broadcast when the download is completed. See the following answer and the code example linked there for reference.

DownloadManager.ACTION_DOWNLOAD_COMPLETE broadcast receiver receiving same download id more than once with different download statuses in Android

Community
  • 1
  • 1
headuck
  • 2,763
  • 16
  • 19