使用相同的Intent Service并行下载



我有一个Intent Service处理下载和解压缩一些文件。现在我尝试使用相同的Intent Service来下载同步文件,但当我尝试启动第二次下载时,它只是排队新的下载,我怎么能不排队我的下载?

Intent service = new Intent(getActivity(), DownloadAndUnzipService.class);
service.putExtra("post", false);
service.putExtra("url", htmlUrlDownload);
service.putExtra("filePath", filePath);
service.putExtra("destinationPath", destinationPath);
service.putExtra("receiver", mReceiver);
service.putExtra("typeDownload", Constants.HTML);
service.putExtra("metaDado", downloadContent.getMetaDado());
service.putExtra("isResuming", false);
getActivity().startService(service);

我没有放DownloadAndUnzipService代码,因为它太长了,我认为它不会有帮助,但如果有帮助,我可以更新它

IntentService是专门设计用于将多个调用排队,并一次运行一个。

。它工作100%正确,但不幸的是你的设计是错误的。

您应该研究像ThreadPoolExecutor这样的东西,它将使您能够从线程池中同时运行多个Runnables。点击这里查看Android文档。

您还将看到一些很好的示例代码。另外,我知道Mark Murphy的电子书有一些很棒的并行下载代码。

我不确定你是否需要把它放到实际的Service中,或者你是否可以把它放在你的Activities中——这取决于你的确切目标。

如果DownloadAndUnzipService是IntentService的子类,那就是预期的行为。

在:Extends an Intent Service

如果你想并行下载,你需要从Service类扩展,并自己管理后台线程

最新更新