【问题标题】:Android DownloadManager remove() - how to determine when the remove() operation finishes?Android DownloadManager remove() - 如何确定 remove() 操作何时完成?
【发布时间】:2014-12-03 20:10:15
【问题描述】:

是否有一种简单的机制来确定 DownloadManager remove() 何时完成,因为它似乎是部分异步的。该函数几乎立即返回并计算下载表中已删除的条目数,但实际的文件系统管理似乎被推入了某个后台线程。

问题是我编写了一些代码,在提取新副本之前查找并删除文件 X 的任何现有 DownloadManager 条目(希望是文件系统对象)。不幸的是,新副本在文件系统管理开始之前就已进入目录,对于以前的化身。因此,内部管理实际上最终会在某个时候删除新版本,并在 DownloadManager 表中留下一个孤立条目。

可以通过某种方式阻止直到文件系统删除被操作。

调试代码:

    DownloadManager.Query query = new DownloadManager.Query().setFilterByStatus(DownloadManager.STATUS_SUCCESSFUL);
    downloads = getAllDownloadIds(manager.query(query));

    path = activity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);

    //If a file name was passed remove any existing entry / version of the file
    if ( fileName != null && ! fileName.isEmpty() ){
        if ( downloads.containsKey(fileName)){
            ids = downloads.get(fileName);
            for (Long id : ids) {
                Uri path = manager.getUriForDownloadedFile(id);
                File checkFile = new File(path.toString());
                Log.e(TAG, "Removing existing file: " + path.toString() + ":" + id);
                int entriesRemoved = manager.remove(id);
                Log.e(TAG, "Existing files removed: " + entriesRemoved);                  
            }
        }
    }


...
Log.v(TAG, "Attempting to create a file in the 'Download' directory on the external storage::" + path.toString() +"/"+ fileName);
file = new File(path, fileName);
Log.v(TAG, "Does the file already exist::" + file.exists());

示例输出:

… V/Export﹕ Removing existing file: file:///storage/sdcard/Download/appData.csv:101
… V/Export﹕ Existing files removed: 1
… V/Export﹕ Attempting to create a file in the 'Download' directory on the external storage::/storage/sdcard/Download/appData.csv
… V/Export﹕ Does the file already exist::true

【问题讨论】:

  • 把你的目的地放在外部存储包文件夹中的一个临时文件夹中,然后自己删除。

标签: android android-download-manager download-manager


【解决方案1】:

我也遇到了同样的问题 - 在快速网络中替换小文件时,替换文件有时会在调用 DownloadManager.remove(...) 后的几分之一秒内到达,在这种情况下,新到达的文件将被删除。

我使用的解决方案是,在调用DownloadManager.remove(...) 之前,我设置了FileObserver 来监控文件。然后我调用 remove(...),然后等待 DELETE 事件触发,然后再开始替换下载。

这最终导致大量代码分布在多个类中。还有其他复杂的因素——例如,我设置了一个超时机制,以防下载管理器永远不会删除文件。 (我无法想象为什么它不会,但它不是我的组件)。

所以在回答“是否有一种简单的机制.....”这个问题时:您可以使用一些机制,但不幸的是不是特别简单的。

【讨论】:

    【解决方案2】:

    我解决这个时间问题的方法就是在 DownloadManager.remove 函数之前删除文件。无论如何,“删除”功能都会删除对下载的引用。

    int idFromCursor = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_ID));
    String localPathOfFile =cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));
    File fileToDelete= new File(localPathOfFile);
    
    if(fileToDelete.exists()){
       fileToDelete.delete();
    }
    downloadManager.remove(idFromCursor);
    

    之后就没有时间问题了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-16
      • 2013-09-01
      • 2012-07-23
      • 1970-01-01
      • 1970-01-01
      • 2014-09-27
      • 2016-11-07
      • 2015-10-14
      相关资源
      最近更新 更多