2012-09-28 95 views
0

我還沒有找到任何關於此的信息。 是否有可能偵聽下載管理器請求狀態更改? 我排隊幾個文件下載,並顯示在下載狀態的列表中。我只在DownloadManager.ACTION_DOWNLOAD_COMPLETE上收到廣播,但我想收到一些通知或設置一些監聽器,以便能夠跟蹤下載請求更改。我想要顯示狀態 - 添加到隊列中,下載,下載...Downloadmanager請求狀態更改通知

我看到的唯一方法是查詢下載管理器,並檢查有關狀態的每個請求,但我需要在listadapter getview方法中執行它,它不會很高效。

回答

2

根據官方文件,這樣做並不是一個好方法。 http://developer.android.com/reference/android/app/DownloadManager.html

我會建議啓動一個服務或某種後臺線程,每隔幾秒查詢下載的狀態。 (保持在一個長[]跟蹤它們的ID,因爲setFilterById需要很長的[])

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將保持平穩。然而,我並不知道更好的方法,對不起,我無法提供不涉及查詢的答案。