【问题标题】:Android ProgressBar in ListView while using DownloadManager使用 DownloadManager 时 ListView 中的 Android ProgressBar
【发布时间】:2014-10-16 07:55:22
【问题描述】:

我有一个 ListView,其中每个项目代表一个 PDF 文件。当用户单击一个项目时,应用程序必须将文件下载到外部存储上。 现在下载不能正常工作,但这不是问题。我希望在下载文件时在列表的每个项目旁边显示一个进度条,旋转轮样式。

我的问题是:我找不到如何让纺车出现。在下载管理器之前,我使用 AsyncTask 进行了尝试,并且旋转的轮子工作了。

这是我的代码:

CategoryActivity.java(ListView的Activity)

@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);

    }
}

请注意,如果我不让纺车消失,单击该项目后它将始终可见。用线来隐藏它,它甚至没有出现。


编辑:
我添加了一个广播接收器。

将这两行放在 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);
    }

编辑 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;
                }
            }

        }
    };

问题是,旋转轮仅隐藏在 listView 中单击的最后一个项目。我尝试使用调试模式,但程序的行为正确(意味着每次下载都会调用loader.setVisibility(View.GONE))。我不知道为什么除了最后点击的项目之外,纺车不会隐藏。


编辑:3 我知道为什么除了最后点击的项目之外,纺车不会隐藏。

当我点击多个项目时,lastDownload 获取最后一次点击的 id。因此,在广播接收器中,它会计算最后一次点击的项目乘以点击的项目数。
我尝试将 lastDownload 更改为 long 的数组/表,并将其与 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 方法中。我认为我不能把它放在其他任何地方,因为它所在的视图不是活动的视图。

长话短说:我必须找到一种方法来访问我点击的项目/视图的进度条,因为我知道我可以在第一个项目到达广播接收器之前点击多个项目。


编辑:4好的,我这样做了:

yzld 在开头设置为 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;
                    }
                }
            }
        }
    }
};

工作正常,除非在下载大文件的同时单击小文件的项目。小文件优先,下载管理器不再按照表格顺序,导致加载轮不消失。


编辑:5

我找到了一种方法来做我想做的事,看看我的答案。

感谢您的帮助。

【问题讨论】:

标签: android android-listview android-progressbar download-manager android-download-manager


【解决方案1】:

好的,所以我设法用 HashTable 做我想做的事情:

// HashTable to store download id's and loaders
Hashtable<Long, SyncedProgressBar> storeTable = new Hashtable<Long, SyncedProgressBar>();

在我的 onClickListener 方法中,loader[z]lastDownload[ld] 取值后,我将它们放入 HashTable 中:(下载 id 将是键,加载器将是值)

// Storing download id and loader in HashTable
storeTable.put(lastDownload[ld], loader[z]);

在我的广播接收器的 onReceive 方法中,不要这样做:

if(lastDownload[y] == referenceId)

我查看 HashTable 是否包含意图的下载 id:

if(storeTable.containsKey(referenceId))

我在加载器中输入了正确的值:

loader[y] = storeTable.get(referenceId);

然后我只需要将加载程序的可见性放在我想要的GONE 上。这个解决方案对我有用,但如果我发现新的东西,我会更新它。

这是我的新代码:

在 onClickListener 方法中:(此处不完整)

// 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));

// Storing download id and loader in HashTable
storeTable.put(lastDownload[ld], loader[z]);

// 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 HashTable contains the Key-download-id for which the method is called
            if(storeTable.containsKey(referenceId)){
                // Loader takes the value for the key
                loader[y] = storeTable.get(referenceId);
                // We define a cursor depending on the id
                Cursor c = mgr.query(new DownloadManager.Query().setFilterById(referenceId));
                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;
                    }
                }
            }
        }
    }
};

【讨论】:

  • 嗨,你能提供完整的解决方案吗,我想做类似的任务,但我想显示每次下载的进度和当前正在下载的文件数
  • SyncedProgressBar 类是什么?
【解决方案2】:

请注意,DownloadManager.enqueue 是异步的,这意味着 mgr.enqueue 几乎立即返回,然后您当前的代码将 spinner 设置为不可见。

要隐藏微调器,您必须注册广播接收器以在下载完成时接收通知。然后,您必须找到相应的微调器并将其隐藏。请注意,下载可能会失败(在这种情况下仍会发送通知)。

CommonsWare 发布了一个example,展示了如何使用 DownloadManager。

【讨论】:

  • 添加了广播接收器。现在,当我单击相应的项目时,微调器变得可见,但其中一些在下载完成后仍然可见。
  • 我编辑了我的问题,但还没有实现下载失败案例。
  • 您的代码仅对最后点击的项目隐藏微调器。那是因为 loader 在 onItemClick 中获取了他的值。您必须存储 mgr.enqueue 返回的下载 ID。在广播接收器中收到的意图包含刚刚完成的下载的 id。使用此信息,您必须找到要禁用的微调器。
  • 好的,如果我按照您提供的链接进行操作,我可以将下载 ID 存储在带有 lastDownload = mgr.enqueue(...); 的变量中。然后在BroadcastReceiver中我想我必须使用setFilterById(lastDownload),但我不知道在那种情况下将loader.setvisibility(View.GONE)放在哪里
【解决方案3】:

我会为此使用 AsyncTask,并让轮子在 onPreCreate 和 onPostCreate 中出现/消失

private class MyDownloader extends AsyncTask<String, Void, File>{

    @Override
    protected void onPreExecute() {
        loader.setVisibility(View.VISIBLE);
    }

    @Override
    protected File doInBackground(String... params) {
        //Download and save file here, and return the result, which will be fed into the onPostExecute method
        return file;
    }

    @Override
    protected void onPostExecute(File file) {
        super.onPostExecute(file);
        loader.setVisibility(View.GONE);
    }
}

然后用 new MyDownloader().execute("linktopdf"); 启动任务或类似的东西(你放入 .execute 的内容将被输入到 doInBackground 方法中)

【讨论】:

  • 请注意,您必须在 doInBackground 中以同步方式下载文件,这样它就会阻塞 AsyncTask。
  • 不能这样做,用户必须能够同时下载多个文件(或点击多个文件,他们一次下载一个),并且必须能够在下载过程中做其他事情.我已经尝试过使用 AsyncTask,但也存在旋转轮问题(有时不会消失,有时会克隆到屏幕外的另一个项目上)
猜你喜欢
  • 2023-03-25
  • 1970-01-01
  • 2012-04-16
  • 1970-01-01
  • 1970-01-01
  • 2015-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多