Reputation: 45
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?
Upvotes: 2
Views: 1848
Reputation: 417
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));
}
}
Upvotes: 1