3

I am downloading a file from the net and then reading it using a BufferedReader. Currently I download the file using DownloadManager then have this code :

while (latestNumbersFile.exists() != true) {
            System.out.println("WAITING...");
        }

        try {
            Thread.sleep(5000);
        } catch (InterruptedException ignored) {
        }

So that any following code does not execute until the file has finished downloading. This works but seams really dirty to me. If I remove the Thread.sleep(5000); it seams that the file does indeed exist but is not quite ready to be read.

There has to be a better way of recognizing when a file not only exists but is actually complete and ready to have something preformed on it.

Any thoughts?? (sorry if this is a noobie question, I've searched high and low for an answer)

Psyonic
  • 195
  • 9

2 Answers2

4

From Android developers when download completes, DownloadManager broadcasts an intent. You can capture this "event" and handle it like:

BroadcastReceiver receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {


             /** Do your coding here **/       

            }
        }
    };

    registerReceiver(receiver, new IntentFilter(
            DownloadManager.ACTION_DOWNLOAD_COMPLETE));

Source from: Vogella

Please let me know if this helps you, if it doesn't we can try anything else :)

Sherekan
  • 536
  • 3
  • 14
  • Thanks Sherekan, gimme a bit, I'll let you know – Psyonic Jul 24 '14 at 10:00
  • @Ash if this helped you please mark the answer as accepted so everyone looking for something like this can rely on this post. – Sherekan Jul 24 '14 at 10:57
  • Sorry mate. I'm trying to understand your code. (still a noob at this) but sleep and work got in the way. Once I understand your code and get it working (or not) I'll give this the tick or ask for more help... – Psyonic Jul 25 '14 at 08:47
  • Okey, don't worry, we all know what you're talking about with that "sleeping" thing :) – Sherekan Jul 25 '14 at 10:05
  • Got some results. I cant get the `registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));` line to work. I've read everything on the Android Developers site I can about it but still don't get it. For me, in Android Studio, registerReceiver gives error `cannot resolve method` but I do have `import android.content.Context;`. (Without **completely** understanding your code) I am calling your code in its own method and at `/** Do your coding here **/` I am simply writing to logcat with `System.out.println("GOT INTO BroadcastReceiver !!");` but nothing is written...? – Psyonic Jul 25 '14 at 10:09
  • 1
    registerReceiver must be called inside an Activity, make sure the class you're using extends from Activity. Be sure too that you're not calling registerReceiver inside the braces on new BroadcastReceiver { ... }. Let me know if it works – Sherekan Jul 25 '14 at 10:12
  • Cool, I'll give that a go. BTW, yeah, having to sleep gets annoying sometimes!! lol :) – Psyonic Jul 25 '14 at 10:18
  • Moved `your code` into the activity and got the `System.out.println("GOT INTO BroadcastReceiver !!");` to print! I think I know where this is going now. I'll re-code to get what I want and let you know... – Psyonic Jul 25 '14 at 10:26
  • WooHoo! All works **GREAT** !! Thanks for sticking with me on this! You get the **BIG** tick! Thank you **SO** much! – Psyonic Jul 25 '14 at 10:43
  • I want others to "get" this. I think the key to this code is the line `if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action))` Importantly, the `if`, so, when `ACTION_DOWNLOAD_COMPLETE` is recognized then the code in the `if` statement can be run. Is that the general concept here @Sherekan? – Psyonic Jul 25 '14 at 11:00
  • Well more or less, "action" variable contains the action (or event, to understand it better) that just happened, and you want to check if that event is the "DOWNLOAD_COMPLETE" event, comparing them with equals make that check and then whatever you run inside that "if" will only run once the download is completed. Glad you got it working :) – Sherekan Jul 25 '14 at 11:27
0

Busy-waiting is not recomended. You'd better use DownloadManager.

From Download a file with Android, and showing the progress in a ProgressDialog :

Use DownloadManager class (GingerBread and newer only) This method is awesome, you do not have to worry about downloading the file manually, handle threads, streams, etc. GingerBread brought a new feature: DownloadManager which allows you to download files easily and delegate the hard work to the system.

Community
  • 1
  • 1
ThomasEdwin
  • 2,035
  • 1
  • 24
  • 36