Android DownloadManager - 如何中止下载?

时间:2014-02-22 15:23:16

标签: android android-download-manager

我需要中止所有失败/暂停/挂起/正在运行的下载,但保留成功的下载。我认为这是通过DownloadManager查询完成的,但我无法弄清楚如何编写代码。请帮忙。

3 个答案:

答案 0 :(得分:1)

我们应该调用setFilterByStatus而不是setFilterById。

>> [M(idxe1Matches), M(idxe2Matches)]

ans = 

    'ABC'    'GHI'
    'ABC'    'GHI'
    'ABC'    'GHI'
    'ABC'    'GHI'
    'ABC'    'GHI'
    'ABC'    'GHI'
    'ABC'    'GHI'
    'ABC'    'GHI'
    'ABC'    'GHI'
    'ABC'    'GHI'
    'ABC'    'GHI'

答案 1 :(得分:0)

这样做 downloadManager.remove(_yourdownloadID);

答案 2 :(得分:0)

根据文档说明,您可以致电remove(downloadID...)

  

取消下载并从下载管理器中删除它们。如果每次下载都在运行,它将被停止,并且将无法再通过下载管理器访问它。如果有下载的文件,部分或完整,则会被删除。

文档:http://developer.android.com/reference/android/app/DownloadManager.html#remove(long...)

Query query = new Query();    
query.setFilterById (DownloadManager.STATUS_FAILED|DownloadManager.STATUS_PENDING|DownloadManager.STATUS_RUNNING);
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Cursor c = dm.query(query);
while(c.moveToNext()) {
    // Here you have all the downloades list which are running, failed, pending
    // and for abort your downloads you can call the `dm.remove(downloadsID)` to cancel and delete them from download manager.
    dm.remove(cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_ID)))
}