I have an Android app that has a feature of downloading files. After using the app for some time, downloading files not working.
I came to know after some research, after a clear cache of download manager of the system, downloading files work completely without any issue.
That means this solution works perfectly. But that doesn't mean application users have to do that to use the app. So I want to do that using code.
I need any solution that can clear the cache of Download Manager of the system using the code.
Here it is template code for downloading:
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);
}
};