You can get a list of installed apps and delete cache like:
public static void clearALLCache()
{
List<PackageInfo> packList = getPackageManager().getInstalledPackages(0);
for (int i=0; i < packList.size(); i++)
{
PackageInfo packInfo = packList.get(i);
if ( (packInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0)
{
String appName = packInfo.applicationInfo.loadLabel(getPackageManager()).toString();
try {
// clearing app data
// Runtime runtime = Runtime.getRuntime();
// runtime.exec("pm clear "+packInfo.packageName);
Context context = getApplicationContext().createPackageContext(packInfo.packageName,Context.CONTEXT_IGNORE_SECURITY);
deleteCache(context);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public static void deleteCache(Context context) {
try {
File dir = context.getCacheDir();
deleteDir(dir);
} catch (Exception e) {}
}
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();
} else if(dir!= null && dir.isFile()) {
return dir.delete();
} else {
return false;
}
}
It is equivalent to clear data option under Settings --> Application Manager --> Your App --> Clear data. (In comment)
For making your application support this you should also follow Privileged Permission Whitelisting from google