Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear user own apps data programmatically

Tags:

android

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?

like image 753
Boardy Avatar asked Feb 07 '11 22:02

Boardy


People also ask

Can I clear user data on apps?

Proceed to open the app listing whose cache and data you want to clear. Select Storage and cache. From there, select Clear cache to clear cache data. For clearing app data and cache, tap the Clear storage button.

What happens when Clear app data?

When you clear data or storage of an app, it deletes the data associated with that app. And when that happens, your app will behave like a freshly installed one. You will have to log in again, grant necessary permissions, change notification settings, and similar things.


2 Answers

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();
}
like image 141
jithu Avatar answered Oct 07 '22 22:10

jithu


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.

like image 25
CommonsWare Avatar answered Oct 08 '22 00:10

CommonsWare