14

I am developing an app where I want to allow the user to be able to set up that if they fail the login after a couple of attempts into the app, it will automatically delete all the data including the preferences and databases.

Is there a simple way to do this or do I have to write code to manually reset everything the app uses?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Boardy
  • 35,417
  • 104
  • 256
  • 447

2 Answers2

9

try this code .

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();
}
NOT_A_PROGRAMMER
  • 1,794
  • 2
  • 20
  • 31
jithu
  • 706
  • 1
  • 9
  • 13
7

Is there a simple way to do this or do I have to write code to manually reset everything the app uses.

You have to write code to manually reset everything the app uses. This should just be a matter of deleting a handful of files. Make sure your database is closed before you try deleting it.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • You can refer to the link and it will show you a simplest way to clear the data http://stackoverflow.com/a/42228794/1252158 – Summved Jain Feb 14 '17 at 14:50