[英]How to find name of file being downloaded using Download Manager
DownloadManager是后台服務。 我想獲取當前正在使用DownloadManager下載的文件的列表。
假設在活動A中啟動了下載過程,而我打開了活動B。在活動BI中,我想知道正在下載哪個URL /文件。 如何實現呢? 如果要下載多個文件,那么如何獲取列表?
您可以在廣播接收器中執行以下操作,以識別已下載的文件。 用DownloadManager實例替換YOUR_DM。
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
}
}
}
}
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.