Clear the app data programmatically in android


Clear the App Data Programmatically in Android


Application data has been created due to use shared preference data, databases and network caches data. This data has been manually clear on Settings -- > Apps (or) Application Manager --> Select the app you want to clear the data. --> Then click button clear data to erase the app from the Phone and SDCARD.

Applications like facebook, google+, gmail and some games captures more data on phone and SDCARD.

Once you clear the data of your app, all passwords and saved settings in app has been lost. So carefull to use this method.

Create the Class MyApplication

public class MyApplication extends Application {
 private static MyApplication instance;
 @Override
 public void onCreate() {
  super.onCreate();
  instance = this;
 }
 public static MyApplication getInstance(){
  return instance;
 }
 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();
 }

}


Then implement it, click events or load event or any more

MyApplication.getInstance().clearApplicationData();

Comments

Popular posts from this blog

convert an integer to a string in java

Print the large string values in android log cat.