How to clear the cache of every application in my android phone programmmatically? does the clearing of cache programmatically is allowed in android? if allowed, how? I already try to researched it and I can't find the answer that I need
Tap Clear data to clear app data and cache. You'll then have to select whether you want to clear all data associated with the app or just its cache. Clearing data will automatically clear the cache of the app as well.
If you are looking for delete cache of your own application then simply delete your cache directory and that's all. By clicking on clear cache button in your code, call deleteCache(this) method.
I've found this one:
import java.io.File;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
public class HelloWorld extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle *) {
super.onCreate(*);
setContentView(R.layout.main);
}
@Override
protected void onStop(){
super.onStop();
}
//Fires after the OnStop() state
@Override
protected void onDestroy() {
super.onDestroy();
try {
trimCache(this);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void trimCache(Context context) {
try {
File dir = context.getCacheDir();
if (dir != null && dir.isDirectory()) {
deleteDir(dir);
}
} catch (Exception e) {
// TODO: handle exception
}
}
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;
}
}
}
// The directory is now empty so delete it
return dir.delete();
}
}
It may be helpful to you.
This is a funny scenario. In the Manifest.Permission documentation
public static final String CLEAR_APP_CACHE
Added in API level 1 Allows an application to clear the caches of all installed applications on the device.
Constant Value: "android.permission.CLEAR_APP_CACHE"
So you can get the permission to clear cache of all application. But I don't think there is any method in the SDK to use this permission. So you can just hold the permission and do nothing with it. Strange from google.
EDIT : This google discussion might be of interest. Dianne Hackborn specifically says that the above permission shouldn't be present in the SDK, since the API to use it is not there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With