1

我有一个具有下载文件功能的 Android 应用程序。使用该应用程序一段时间后,无法下载文件。

经过一番研究,我知道,在系统下载管理器的清晰缓存后,下载文件完全没有任何问题。

这意味着该解决方案可以完美运行。但这并不意味着应用程序用户必须这样做才能使用该应用程序。所以我想用代码来做到这一点。

我需要任何可以使用代码清除系统下载管理器缓存的解决方案。

这是用于下载的模板代码:

IntentFilter filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
context.registerReceiver(downloadReceiver, filter);

downloadManager = (DownloadManager) context.getSystemService(DOWNLOAD_SERVICE);

Uri Download_Uri = Uri.parse(url);

DownloadManager.Request request = new DownloadManager.Request(Download_Uri);

//Restrict the types of networks over which this download may proceed.
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);

//Set whether this download may proceed over a roaming connection.
request.setAllowedOverRoaming(false);

//Set the title of this download, to be displayed in notifications (if enabled).
request.setTitle(savedFileName);

//Set a description of this download, to be displayed in notifications (if enabled)
//equest.setDescription("Android Data download using DownloadManager.");
//Set the local destination for the downloaded file to a path within the application's external files directory
request.setDestinationInExternalPublicDir(download_path, savedFileName);

//Set visible and shows in the notifications while in progress and after completion.
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

//Enqueue a new download and same the referenceId
downloadReference = downloadManager.enqueue(request);

// receiver
private BroadcastReceiver downloadReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {

        DownloadManager.Query query = new DownloadManager.Query();
        query.setFilterById(downloadReference);
        Cursor c = downloadManager.query(query);

        if (c.moveToFirst()) {
            checkStatus(c);
        }
};
4

0 回答 0