2011-02-07 57 views
12

我正在开发一个应用程序,我想让用户能够设置,如果他们在尝试进入应用程序后尝试登录失败,它会自动删除所有数据,包括偏好和数据库。以编程方式清除我自己的应用程序数据

是否有一个简单的方法来做到这一点,或者我必须编写代码来手动重置应用程序使用的所有内容。

感谢这个

回答

6

任何帮助,有一个简单的办法做到这一点还是我必须编写代码来手动复位应用程序使用的一切。

您必须编写代码来手动重置应用程序使用的所有内容。这应该只是删除少数文件的问题。确保您的数据库在尝试删除之前已关闭。

+0

冷静的帮助表示感谢,认为这可能是你可以参考链接的情况下 – Boardy 2011-02-07 23:09:45

+0

,它会告诉你清除数据的最简单的方法http://stackoverflow.com/a/42228794/1252158 – 2017-02-14 14:50:11

9

试试这段代码。

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(); 
} 
相关问题