9

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 Answers3

11

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.

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
4

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.

Krishnabhadra
  • 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
0

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();
}
Lena Bru
  • 13,521
  • 11
  • 61
  • 126
Sushil
  • 8,250
  • 3
  • 39
  • 71