2

I successfully created a Download Manager and a broadcast intent action sent by the download manager when the download completes: using this Android download manager complete

But, I use the download manager to download not 1 file, but about 5-10 files whenever and this intent:

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

BroadcastReceiver onComplete=new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
    // your code
}

starts everytime a single download is completed.

How do I send an Intent, when all the files are downloaded?

Community
  • 1
  • 1
Simo
  • 45
  • 7
  • Keep a list of all files downloading. Mark when they're done (use a file or db to do this as your app may be killed). When there's no files left marked undone, you've downloaded all the files. A db table would be good for this. – Gabe Sechan Mar 22 '16 at 08:11
  • 1
    There isn't any event, associated to Download Manager? I implemented it to a custom class, so your solutions are too complex to implement – Simo Mar 22 '16 at 08:22
  • use `DownloadManager#query()` to see the status of your downloads (`DownloadManager#STATUS_*`) – pskink Mar 22 '16 at 14:27

1 Answers1

1

Create a class say MyDownloadManager for your downloading code and every time you need to download a new file call a new instance of MyDownloadManager class. Android's default DownloadManager will handle this automatically and start download of multiple files.

private void downloadFile(String url){
        MyDownloadManager downloadManager = new MyDownloadManager();
        downloadManager.DownloadFile(getApplicationContext(), url);
    }

Your MyDownloadManager class will be like this:

public class MyDownloadManager{
private Context mContext;
private String url;

public void Download(Context context, String url){
    mContext = context;
    this.url = url;
    String serviceString = Context.DOWNLOAD_SERVICE;
    DownloadManager downloadManager;
    downloadManager = (DownloadManager)mContext.getSystemService(serviceString);    
    DownloadManager.Request request = new DownloadManager.Request(uri);
    long reference = downloadManager.enqueue(request);
}

public void RegisterDownloadManagerReciever(Context context) {
    BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
                // Do something on download complete
            }
        }
    };
    context.registerReceiver(receiver, new IntentFilter(
            DownloadManager.ACTION_DOWNLOAD_COMPLETE));
    }
}
Deepak Sharma
  • 417
  • 3
  • 9
  • 1
    This example is a simple DownloadManager, the event is calling everytime a single download is completed. For example, if I call "downloadFile" 6 times, I've got six events – Simo Mar 30 '16 at 06:38
  • Download Manager is registered in the last line of MyDownloadManager class. It is a good thing that the event gets fired 6 times because that way you can download multiple files simultaneously. – Deepak Sharma Apr 01 '16 at 10:08