1
我有一個ListView
由項目填充。這些項目代表我可以通過點擊下載的文件。當我點擊一個項目時,啓動download manager
,並將文件下載到我創建的目錄中。Android讓下載管理器一次下載一個文件?
正在下載文件時,不確定的progress bar
(加載輪)在列表中的相應項目上可見。
我的問題是:因爲當他們的物品被點擊時(第一個物品點擊,第二個物品點擊,第五個物品點擊等等),我認識到裝載輪子(隱藏他們),我不想download manager
同時下載多個文件。
例如:如果我點擊一個大文件,然後點擊列表中的一個小文件,我想在開始下載小文件之前完整下載大文件。
有沒有辦法做到這一點?
編輯:這裏是我的代碼
y
,z
和ld
在開始時設置爲0
。
當被點擊的項目:
// Loader of the clicked item is made visible
loader[z].setVisibility(View.VISIBLE);
// Construction of the URL
SharedPreferences codeSaveUrl = getSharedPreferences(PREFS_TEXT,Context.MODE_PRIVATE);
url2 = codeSaveUrl.getString("defaut", ""); // Organization code
uri = url10 + url2 + "&file=" + udl ;
// URL parse to URI
Uri myuri = Uri.parse(uri);
// Enqueue file to downloads, with notification. Storage of download id in a table
lastDownload[ld] = mgr.enqueue(new DownloadManager.Request(myuri)
.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI |DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false)
.setTitle(udl + ".pdf")
.setDescription("Téléchargement en cours")
.setDestinationInExternalPublicDir("/Protocols/", (udl+".pdf"))
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE));
// Increment variables for next downloads
ld=ld+1;
z=z+1;
廣播接收器:
// Broadcast Receiver called when a download is finished
BroadcastReceiver onComplete = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
// referenceId is the download's id for which the method is called
long referenceId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
// If y (=0 at the beginning) is inferior to the number of downloads
if(y <ld){
// If the id of the download corresponds to the one for which the method is called
if(lastDownload[y] == referenceId){
// We define a cursor depending on the id
Cursor c = mgr.query(new DownloadManager.Query().setFilterById(lastDownload[y]));
if(c.moveToFirst()){
// Download status recovery
int x = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
switch(x){
// If download is paused, pending or running, we do nothing
case DownloadManager.STATUS_PAUSED:
case DownloadManager.STATUS_PENDING:
case DownloadManager.STATUS_RUNNING:
break;
// If file has successfully been downloaded, loader is hidden
case DownloadManager.STATUS_SUCCESSFUL:
loader[y].setVisibility(View.GONE);
// Increment y to go to next download
y=y+1;
break;
// If download failed, it is retried
case DownloadManager.STATUS_FAILED:
//TODO: retry download
break;
}
}
}
}
}
};
感謝您的幫助。