下载完成后,我成功创建了下载管理器发送的下载管理器和广播意图操作:使用此{{3}}
但是,我使用下载管理器下载的不是1个文件,而是每次约5-10个文件和这个意图:
registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
BroadcastReceiver onComplete=new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
// your code
}
每次下载完成后就会启动。
如何下载所有文件,我该如何发送意图?
答案 0 :(得分:1)
为您的下载代码创建一个名为MyDownloadManager的类,每次需要下载新文件时,都要调用MyDownloadManager类的新实例。 Android的默认DownloadManager将自动处理并开始下载多个文件。
private void downloadFile(String url){
MyDownloadManager downloadManager = new MyDownloadManager();
downloadManager.DownloadFile(getApplicationContext(), url);
}
您的 MyDownloadManager 类将如下所示:
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));
}
}