I'm trying to develop an android app that could erase others application cache data, I tried to browse through all blogs but none of them worked for me, I can able to clear my application's cache by the following code
File cache = getCacheDir();
File appDir = new File(cache.getParent());
if (appDir.exists())
{
String[] children = appDir.list();
for (String s : children)
{
if (!s.equals("lib"))
{
deleteDir(new File(appDir, s));
Toast.makeText(DroidCleaner.this, "Cache Cleaned", Toast.LENGTH_LONG).show();
Log.i("TAG", "**************** File /data/data/APP_PACKAGE/" + s + " DELETED *******************");
}
}
}
public static boolean deleteDir(File dir)
{
if (dir != null && dir.isDirectory())
{
String[] children = dir.list();
for (int i = 0; i < children.length; i++)
{
boolean success = deleteDir(new File(dir, children[i]));
if (!success)
{
return false;
}
}
}
return dir.delete();
}
My manifest code
<uses-permission android:name="android.permission.CLEAR_APP_CACHE"/>
I tested the code on 2.2, 2.3 and 4.0
and after seeing the post in the following link Android: Clear Cache of All Apps?
I changed my code to
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;
}
}
I want to clear other application's cache, can any body please help me, please correct me if I'm wrong, Thanks in advance.
See Question&Answers more detail:os