1

我还没有找到任何关于那个的信息。是否有可能监听下载管理器请求状态的变化?我排队下载几个文件,并以下载状态将其显示在列表中。我只在 DownloadManager.ACTION_DOWNLOAD_COMPLETE 上接收广播,但我想接收一些通知或设置一些侦听器,以便能够跟踪下载请求的更改。我想要显示状态 - 添加到队列、下载、下载...

我看到的唯一方法是查询下载管理器并检查有关状态的每个请求,但我需要在 listadapter getview 方法中执行此操作,并且效率不高。

4

1 回答 1

2

根据官方文档,没有这样做的好方法。 http://developer.android.com/reference/android/app/DownloadManager.html

我建议要么启动一项服务,要么启动某种后台线程,每隔几秒钟查询一次下载的状态。(在 long[] 中跟踪他们的 id,因为 setFilterById 需要 long[])

long[] ids = new long[MyDownloadQueue.length];
// Look at all these requests I'm building!
ids[i++] = dm.enqueue(request);

Cursor cursor = dm.query(new DownloadManager.Query().setFilterById(ids));
while (cursor.moveToNext()) {
  int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
  int status = cursor.getInt(columnIndex);
  int columnReason = cursor.getColumnIndex(DownloadManager.COLUMN_REASON);
  int reason = cursor.getInt(columnReason);
  // your stuff here, recording the status and reason to some in memory map
}

这样,您的列表视图就不必做任何工作,并且 UI 将保持流畅。但是,我知道没有更好的方法,抱歉,我无法提供不涉及查询的答案。

于 2013-01-04T14:16:52.703 回答