5

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?

robosheep
  • 93
  • 2
  • 5

2 Answers2

7

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.

Kishan Dhamat
  • 3,746
  • 2
  • 26
  • 36
Tim Capes
  • 655
  • 2
  • 7
  • 12
  • than @tim can u pls post any code for clearing cache for all the apps? And can one gets the amount of memory released? – AkashG Jan 20 '14 at 08:42
3

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • OK, thanks. I've seen utilities that help you manage your cache though but they must require root access. Is there a way to at least list how much space each app's cache is taking up? Is there some way to jump to the settings activity with "Clear cache" button for a specific app? – robosheep Jan 06 '11 at 09:52
  • @robosheep: "Is there a way to at least list how much space each app's cache is taking up?" -- not that I am aware of, but I haven't exactly gone looking for it. "Is there some way to jump to the settings activity with "Clear cache" button for a specific app?" -- try http://developer.android.com/reference/android/provider/Settings.html#ACTION_APPLICATION_DETAILS_SETTINGS (note: new to Android 2.3) – CommonsWare Jan 06 '11 at 12:38