2012-01-10 82 views
2

有什麼辦法可以實現功能,它將清除我的應用程序中的所有數據/文件/ shared_preferences /數據庫。我需要Android應用設置中的「清除數據」按鈕等功能。Android重置應用程序的功能

任何想法如何實現這一目標?

+0

要清楚,U想從SD卡或共享偏好清除文件。 – 2012-01-10 12:33:39

+0

我想獲得清晰的數據功能。不清除SD卡中的文件。我想重置我的應用程序。清除所有文件/共享首選項/數據庫全部來自內部存儲 – 2012-01-10 12:35:05

回答

2

此鏈接可能會有所幫助你
http://www.hrupin.com/2011/11/how-to-clear-user-data-in-your-android-application-programmatically

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(); 
} 

}

+0

頁面未加載 – 2012-01-10 12:56:45

1

試圖做「DROP DATABASE database_name」?

相關問題