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
3 Answers
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.
 
    
    - 60,504
- 58
- 273
- 437
- 
                    I have 30 pages in my project. If I want to clear the cache how should I do this. I have to add this code in all my the classes or any of one class. – Vijay Apr 28 '15 at 04:50
- 
                    you can extends `Activity` in one class with these methods and then import that class in all activities. – Pratik Butani Apr 28 '15 at 05:03
- 
                    Can this snippet clear the cache of all applications installed on device ? – Sagar Thakarar May 11 '16 at 06:37
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.
 
    
    - 34,169
- 30
- 118
- 167
- 
                    You cannot use this permission, from https://developer.android.com/reference/android/Manifest.permission.html#CLEAR_APP_CACHE - Protection level: system|signature – AndroidCoolestRulest May 22 '16 at 08:13
To clear Application Data please try this way. I think it'll help you.
public void clearApplicationData() 
{
    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));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();
}
 
     
    