Reputation: 1402
I don't think it's a duplicate. Well, I explain what I need.. I've got a list of all application installed in my device.. By a click I need to show a dialog that says "Do you want clear cache?" with "yes" or of course, "no". I found this tutorial: http://android-sample-code.blogspot.it/2012/01/how-to-clear-cache-data-in-android.html but seems to delete the data folder. What I want to know is; is there difference? Is there a code only for clear cache and not data of a application?
Code:
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
/**Clear cache*/
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;
}
}
}
Upvotes: 1
Views: 3970
Reputation: 62419
May this will help you:
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;
}
}
Dont forgot permission :
<uses-permission android:name="android.permission.CLEAR_APP_CACHE"/>
Upvotes: 1