2017-04-15 51 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

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

相关问题