我有一个 ListView,其中每个项目代表一个 PDF 文件。当用户单击某个项目时,应用程序必须将文件下载到外部存储上。
现在下载无法正常运行,但这不是问题所在。我想要一个进度条(旋转轮样式)在下载文件时出现在列表的每个项目旁边。
我的问题是:我找不到如何让旋转轮出现。在使用下载管理器之前,我尝试使用 AsyncTask 并且旋转轮可以工作。
这是我的代码:
CategoryActivity.java(ListView的活动)
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// Récupère les valeurs depuis ListItem
udl = ((TextView) view.findViewById(R.id.udl)).getText().toString();
// This is the spinning wheel
loader = ((ProgressBar) view.findViewById(R.id.spinWheel2));
filepath = dirpath + udl + ".pdf";
File file = new File(filepath);
if (file.exists()) {
// If the file exists, I open it
}else{ // Else I download it
// Setting the spinning wheel to VISIBLE
loader.setVisibility(View.VISIBLE);
SharedPreferences codeSaveUrl = getSharedPreferences(PREFS_TEXT,Context.MODE_PRIVATE);
url2 = codeSaveUrl.getString("defaut", ""); // Code Organisation
// Constructing the uriString
uri = url10 + url2 + "&file=" + udl ;
Uri myuri = Uri.parse(uri);
DownloadManager mgr=(DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE);
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));
// Hiding the spinning wheel
loader.setVisibility(View.GONE);
}
}
请注意,如果我不让旋转轮消失,则单击该项目后它将始终可见。用线隐藏它,它甚至不会出现。
EDIT :
我添加了一个广播接收器。
将这两行放入 onCreate() 中:
final DownloadManager mgr=(DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE);
registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
并添加了这个:
BroadcastReceiver onComplete = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
loader.setVisibility(View.GONE);
}
};
@Override
public void onDestroy(){
super.onDestroy();
unregisterReceiver(onComplete);
}
EDIT 2 :好的,这是我所做的一些更改:
我将下载的 id 保存在变量中:
lastDownload = 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_NOTIFY_COMPLETED));
根据下载的状态,我想做不同的事情:
BroadcastReceiver onComplete = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
Cursor c = mgr.query(new DownloadManager.Query().setFilterById(lastDownload));
if(c.moveToFirst()){
int x = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
switch(x){
case DownloadManager.STATUS_PAUSED:
case DownloadManager.STATUS_PENDING:
case DownloadManager.STATUS_RUNNING:
break;
case DownloadManager.STATUS_SUCCESSFUL:
loader.setVisibility(View.GONE);
break;
case DownloadManager.STATUS_FAILED:
//TODO: retry download
break;
}
}
}
};
问题是,旋转轮仅在列表视图中单击的最后一个项目时隐藏。我尝试使用调试模式,但程序具有正确的行为(意味着loader.setVisibility(View.GONE)
每次下载都会调用)。我不知道为什么除了最后点击的项目之外,旋转轮不会隐藏。
EDIT : 3我知道为什么除了最后点击的项目之外,旋转轮不会隐藏。
当我单击多个项目时,lastDownload 会获取最后一个单击的项目的 ID。因此,在广播接收器中,它执行的是最后单击的项目乘以单击的项目数的情况。
我尝试将lastDownload更改为长数组/表,并将其与referenceId进行比较,我相信它是意图中包含的id。
这是新代码(y=0,ld 是单击的项目数):
BroadcastReceiver onComplete = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
long referenceId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
if(y <ld){
if(lastDownload[y] == referenceId){
Cursor c = mgr.query(new DownloadManager.Query().setFilterById(lastDownload[y]));
if(c.moveToFirst()){
int x = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
switch(x){
case DownloadManager.STATUS_PAUSED:
case DownloadManager.STATUS_PENDING:
case DownloadManager.STATUS_RUNNING:
break;
case DownloadManager.STATUS_SUCCESSFUL:
loader.setVisibility(View.GONE); // This loader is the one of the last item clicked
break;
case DownloadManager.STATUS_FAILED:
//TODO: retry download
break;
}
}
y=y+1;
}
}
}
};
我没有编写将变量放回 0 的部分,但目前代码几乎按预期工作。剩下的唯一问题是我消失的旋转轮是最后单击的项目的旋转轮。我知道为什么。因为这一行:loader = ((ProgressBar) view.findViewById(R.id.spinWheel2));
位于onItemClicked
方法。我认为我不能把它放在其他地方,因为它所在的视图不是活动的视图。
长话短说:我必须找到一种方法来访问我单击的项目/视图的进度栏,因为我知道我可以在第一个项目到达广播接收器之前单击多个项目。
EDIT : 4好的,我这样做了:
y
,z
, and 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;
}
}
}
}
}
};
工作正常,除非在下载大文件时单击小文件的项目。小文件优先,下载管理器不再遵循表格的顺序,导致加载轮不会消失。
EDIT : 5
我找到了一种方法来做我想做的事,请参阅我的答案。
感谢您的帮助。