DownloadManager是一项后台服务。我想获取当前使用DownloadManager下载的文件列表。
假设下载过程在活动A中启动,我打开活动B.在活动B中,我想知道正在下载哪个URL /文件。怎么做到这一点?如果正在下载多个文件,那么我该如何获取列表?
答案 0 :(得分:0)
您可以在广播接收器中执行以下操作,以识别下载的文件。将YOUR_DM替换为您的DownloadManager实例。
receiver_complete = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE){
Bundle extras = intent.getExtras();
DownloadManager.Query q = new DownloadManager.Query();
q.setFilterById(extras.getLong(DownloadManager.EXTRA_DOWNLOAD_ID));
Cursor c = YOUR_DM.query(q);
if (c.moveToFirst()) {
int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
if (status == DownloadManager.STATUS_SUCCESSFUL) {
// process download
title = c.getString(c.getColumnIndex(DownloadManager.COLUMN_TITLE));
// get other required data by changing the constant passed to getColumnIndex
}
}
}
}
};