2011-02-07 58 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(); 
} 
相關問題