我想以编程方式删除我的应用数据。我找到了方法clearApplicationUserData。但是当我运行它时,应用程序最小化了自己。也就是说,应用程序转到后台,就像按下主页按钮一样。这是我的代码:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
((ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE))
.clearApplicationUserData();
} else {
// TODO
}
有一种方法可以使用此方法删除数据,而不会最小化应用程序?
答案 0 :(得分:4)
方法 ActivityManager.clearApplicationUserData()会清除所有应用的数据,并直接终止应用进程,不会发出任何警告。我检查文档和源代码,它似乎不像一个bug,但设计为这样的工作。我有一些猜测如下:
如果您想实施自己的方法来管理应用的数据。 This answer可能会有帮助。
答案 1 :(得分:0)
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));
}
}
}
}
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();
}