问题
I am trying to make an application that will do some cleanup on my device among other things I would like it to delete all of the files residing in my Download
dir. I use a method like this to delete the files:
private static void deleteFiles(File path) {
Util.Log("deleting all files underneath " + path.getName());
if( path.exists() && path.isDirectory() ) {
File[] files = path.listFiles();
for(int i=0; i<files.length; i++) {
if(files[i].isDirectory()) {
Util.Log(files[i].getName() + " is a dir, being recursive.");
deleteFiles(files[i]);
}else {
Util.Log(files[i].getName() + " is a file, deleting it.");
files[i].delete();
}
}
}
}
This is properly deleting the files (I have verified with file manager and plugged in to PC). But if I open up the stock "Downloads" application on my device all of the files are still listed in there. When I click on one I get a popup that says "Downloaded file is not found. [Try again] [Delete]. Pressing try again will re-download the file, pressing delete will delete the entry in the list. I am wondering if there is some broadcast or something that I can use to tell this Downloads application that I want it to "refresh" or "sync" with the current file system so that it will recognize that the files have been deleted.
A similar thing occurs when deleting images out of the DCIM
dir, they would still appear in the stock 'Gallery' app after deleted. I was able to broadcast a MEDIA_MOUNTED intent that caused the Gallery app to "refresh" with the current files (which then properly removed the images from the Gallery app). However MEDIA_MOUNTED doesn't seem to affect the Downloads application.
Is there anything along the same lines that I can do to tell the Downloads app I want it to refresh its list based on the files currently present (or not present) in the /sdcard/Download/
dir?
回答1:
It is actually possible to clear the entries in the Download application. You can simply use the remove(id)
method from the DownloadManager. The only problem is that you probably don't have the referenceId of the downloaded file anymore. If you have the local file Uri or filename, you can simply query the DownloadManager for the successful downloads and look up the id. It seems very cumbersome, but works.
来源:https://stackoverflow.com/questions/14148564/clear-out-android-downloads-list