android - 从以前的应用程序安装中删除所有内容
问题描述
我们已经用所有新功能从头开始重建了我们的应用程序,并希望在用户升级到新版本时删除所有以前的数据。
复杂性:在以前的应用程序中,文件名和数据库名是根据用户帐户值动态生成的,因此我们需要在不知道它们的确切名称的情况下删除它们。
并发症 #2:之前的应用程序是由外部机构完成的。我不知道他们保存的所有东西以及保存在哪里。所以我想在这里发展核能。
这会做吗?我错过了什么吗?有没有更好的办法?提前致谢!
private const val LEGACY_PREFS = "old_shared_prefs"
fun deleteLegacyAppData(context: Context) {
context.cleanUpLegacySharedPrefs()
// this call assumes that the new code base
// with the same package name would retrieve
// the same filesDir
context.cleanUpLegacyData(context.filesDir)
}
private fun Context.cleanUpLegacySharedPrefs() {
val prefs = getSharedPreferences(LEGACY_PREFS, Context.MODE_PRIVATE)
prefs.edit().clear().apply()
}
private fun Context.cleanUpLegacyData(file: File) {
if (file.isDirectory) {
for (f in file.listFiles() ?: emptyArray()) {
cleanUpLegacyData(f)
}
}
file.delete()
}
解决方案
推荐阅读
- javascript - 提交表单后,角度页面导航未重定向
- neo4j - Cypher 与条件 APOC 程序的动态关系?
- excel - 如何使用日期引用查找列?
- python - 用于计算 3 次考试的平均分数和最终成绩的 Python 程序
- ios - 将内容模型转换为 EnvironmentObject
- r - 如何选择特定值并将其插入到 Excel 文件中?
- javascript - 带有显示按钮的 div 容器的弹出框
- powershell - 是否可以获得从特定分支构建的最新 GitHub 版本?
- python - Sparta.py 失败,因为它无法导入 python-elixir
- c++ - C++ - 使用模板传递引用/值