Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clearing Cache in android App Programmatically

I need one help regarding cache memory in my android app. I am Running server (android) in a device. I want to do clear cache of that app programmatically. I have database in that server. Based on that database my client operations are going on. So I don't want it (Database) to get effected. I just want to clear cache not to clear data. Please help me with this.

like image 692
Amith Avatar asked Feb 13 '15 05:02

Amith


1 Answers

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) {
         e.printStackTrace();
      }
   }

   public static void trimCache(Context context) {
      try {
         File dir = context.getCacheDir();
         deleteDir(dir);
      } catch (Exception e) {
         e.printStackTrace();
      }
   }

   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();
      }
      else {
         return false;
      }
   }
like image 142
Aown Raza Avatar answered Sep 28 '22 09:09

Aown Raza