简体   繁体   English

Android中的下载管理器

[英]Download Manager in Android

I'm developing a magazine app and I need to download one edition of the magazine to be available in offline mode. 我正在开发杂志应用程序,需要下载该杂志的一个版本才能以离线模式使用。 One edition may have 20 articles. 一个版本可能有20篇文章。

I'm using download manager to download the articles, enqueueing all of them, but I need a callback when finished to download all articles of one edition. 我正在使用下载管理器来下载文章,将所有文章排入队列,但是下载完一个版本的所有文章后,我需要回调。

If I click to download two magazines at once, it'll enqueue all the articles of both magazines and I need that they be individual. 如果单击一次一次下载两本杂志,它将使两本杂志的所有文章都入队,因此我需要将它们分开。

So far I have this: 到目前为止,我有这个:

mDownloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
for (final Materia materia : materiasList) {
    String url = materia.url;
    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
    request.setDescription("Baixando...");
    request.setTitle(mEditionTitle + " - " + materia.titulo);
    request.allowScanningByMediaScanner();
    request.setDestinationInExternalFilesDir(mContext, "/editions/" + String.valueOf(mEditionId) + File.separator, String.valueOf(materia.id) + ".html");
    // get download service and enqueue file
    mDownloadManager.enqueue(request);
}

EDIT: I'm running this manager from a service. 编辑:我正在从服务运行此管理器。

Not sure this is the best solution but you could query your download manager at regular interval and get all the in progress downloads. 不确定这是最佳解决方案,但您可以定期查询下载管理器并获取所有正在进行的下载。 Then you parse the result and check if it is still downloading some items for a collection or if they are all downloaded. 然后,您解析结果并检查它是否仍在为集合下载某些项目,或者是否全部下载了这些项目。

Query the manager: 查询管理员:

ArrayList<QueuedDownload> downloads = new ArrayList<>();
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Query myDownloadQuery = new DownloadManager.Query();
myDownloadQuery.setFilterByStatus(DownloadManager.STATUS_RUNNING | DownloadManager.STATUS_PAUSED | DownloadManager.STATUS_PENDING);
Cursor cursor = downloadManager.query(myDownloadQuery);
while (cursor.moveToNext()) {
    // Parse each download
}
cursor.close();

Of course that should run on a background thread. 当然应该在后台线程上运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM