title: Android: How to clear cache of all application simultaneously
body: <p>I am very new to android. I want to develop an application for study in which clicking on one button will clear the cache memory of all installed application in android device.I know this type of app is available on google play but I want to develop it myself. I try the following code but it clear the cache memory of current one application and throw Null Pointer Exception. I have a need of clearing cache memory of each and every application simultaneously. please help me to solve this problem.Thanks in advance..</p>
  
  <pre><code>Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
  
      mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
  
      List&lt;ResolveInfo&gt; mainList = getPackageManager().queryIntentActivities(mainIntent, 0);
  
       Context context;
  
  for(ResolveInfo rInfo : mainList)
    {
  
       String packageName = rInfo.activityInfo.applicationInfo.packageName;
  
       context = createPackageContext(packageName, Context.CONTEXT_IGNORE_SECURITY);
  
       clearApplicationData(context);
  
    }
  
  public void clearApplicationData(Context context) {
  
    if(context.getCacheDir()!=null)
          {
  
       File cache = context.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 &amp;&amp; dir.isDirectory()) {
          String[] children = dir.list();
          for (int i = 0; i &lt; children.length; i++) {
              boolean success = deleteDir(new File(dir, children[i]));
              if (!success) {
                  return false;
              }
          }
      }
      return dir.delete();
  } 
  </code></pre>