2017-04-15 47 views
0

下載管理器是在android下載單個文件的最佳方式,它還維護通知欄。但我如何通過它下載多個文件並顯示整個通過通知中的進度條來下載狀態。如何通過Android中的DownloadManager下載Muliple文件(圖像/視頻url)

請爲它或任何代碼段推薦任何庫。

+0

如果我沒有得到你,在一度將入隊的兩個項目默認給你你想要的東西。 –

+0

當我排隊多次,它顯示通知欄中的多個文件下載,我只想一個通知進度欄爲整個多個文件。 –

+0

你試過了什麼?你的代碼在哪裏? –

回答

0

你可能會隱藏DownloadManager的通知並顯示你自己的,應該做你想做的。

禁用setNotificationVisibility(DownloadManger.VISIBILITY_HIDDEN);來隱藏通知。

要顯示下載進度,您可以在DownloadManager的數據庫上註冊ContentObserver以獲取定期更新並使用它更新您自己的通知。

Cursor mDownloadManagerCursor = mDownloadManager.query(new DownloadManager.Query()); 
if (mDownloadManagerCursor != null) { 
    mDownloadManagerCursor.registerContentObserver(mDownloadFileObserver); 
} 

而且ContentObserver看起來像:

private ContentObserver mDownloadFileObserver = new ContentObserver(new Handler(Looper.getMainLooper())) { 
    @Override 
    public void onChange(boolean selfChange) { 
     Cursor cursor = mDownloadManager.query(new DownloadManager.Query()); 

     if (cursor != null) { 
      long bytesDownloaded = 0; 
      long totalBytes = 0; 

      while (cursor.moveToNext()) { 
       bytesDownloaded += cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)); 
       totalBytes += cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)); 
      } 

      float progress = (float) (bytesDownloaded * 1.0/totalBytes); 
      showNotificationWithProgress(progress); 

      cursor.close(); 
     } 
    } 
}; 

並與進步的通知可以顯示:

public void showNotificationWithProgress(Context context, int progress) { 
    NotificationManagerCompat.from(context).notify(0, 
      new NotificationCompat.Builder(context) 
        .setSmallIcon(R.mipmap.ic_launcher) 
        .setContentTitle("Downloading...") 
        .setContentText("Progress") 
        .setProgress(100, progress * 100, false) 
        .setOnGoing(true) 
        .build()); 
} 
+0

感謝您給我一個很好的建議,您可以給我一些下載管理器上的Content Observer的代碼片段來更新通知欄,實際上我是這個主題的新增內容。 –

+0

檢查更新的答案。 –

+1

謝謝,你做了我的一天。 –

相關問題