问题
I want to write a utility where the user can select a set of installed apps and clear their data caches i.e. like the way you can do manually using the built-in Settings->Applictions settings screen with the "Clear cache" button.
How can I access how much cached data each app has and programmatically clear these caches?
回答1:
Ordinary SDK applications have no rights to access, let alone modify, the caches of other applications, any more than they have a right to hack your files.
This may be possible on rooted phones with your application running as root, in which case you will have to manually construct the paths based upon the apps' package names.
回答2:
The answer given here is false, there are market apps which have a programmatic clear all app cache functionality. Furthermore the following is in the documentation:
public static final String CLEAR_APP_CACHE
Since: API Level 1 in android Allows an application to clear the caches of all installed applications on the device. Constant Value:
android.permission.CLEAR_APP_CACHE
Note the "all installed applications portion", and the fact that you don't need any root access or special permissions beyond a standard user permission.
I post here because I have a requirement on a deliverable to clear all app caches every 24 hours on an in store demo phone and I'm trying to figure out how to use this properly. I know that part of the solution is getting this permission, I also know how to find all installed applications on the device, and I'm working on being able to actually delete the cache. My issue is other app caches read as containing no files (despite having content), I suspect because I don't have read access to make the list Files call.
回答3:
You should have this permission in your manifest:
<uses-permission android:name="android.permission.CLEAR_APP_CACHE"/>
After that you can use PackageManager to clear cache of all program:
PackageManager pm = getPackageManager();
// Get all methods on the PackageManager
Method[] methods = pm.getClass().getDeclaredMethods();
for (Method m : methods) {
if (m.getName().equals("freeStorage")) {
// Found the method I want to use
try {
long desiredFreeStorage = 8 * 1024 * 1024 * 1024; // Request for 8GB of free space
m.invoke(pm, desiredFreeStorage , null);
} catch (Exception e) {
// Method invocation failed. Could be a permission problem
}
break;
}
}
This requests that Android clear enough cache files so that there is 8GB free. If you set this number high enough you should achieve what you want (that Android will delete all of the files in the cache). When you call freeStorage() it checks to see if the amount of storage (in this case 8GB) is available for cache files. If not, it starts to delete files from application's cache directories by deleting the oldest files first. It continues to delete files until either there are not longer any files to delete, or it has freed up the amount of storage you requested (in this case 8GB).
来源:https://stackoverflow.com/questions/4605571/clearing-app-cache-programmatically