首页 > 解决方案 > 从以前的应用程序安装中删除所有内容

问题描述

我们已经用所有新功能从头开始重建了我们的应用程序,并希望在用户升级到新版本时删除所有以前的数据。

复杂性:在以前的应用程序中,文件名和数据库名是根据用户帐户值动态生成的,因此我们需要在不知道它们的确切名称的情况下删除它们。

并发症 #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()
}

标签: android

解决方案


推荐阅读