我有什么方法可以实现清除我的应用程序中所有数据/文件/ shared_preferences /数据库的功能。我需要Android App Settings中的Clear Data按钮等功能。
任何想法如何实现这一目标?
答案 0 :(得分: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();
}
}
答案 1 :(得分:1)
尝试“DROP DATABASE database_name”?