【问题标题】:SharedPreferences not being removed on user uninstalling application用户卸载应用程序时未删除 SharedPreferences
【发布时间】:2016-05-03 22:23:51
【问题描述】:

有人在 Nexus 6P 设备上遇到过这个问题吗?我只在 Nexus 6P(运行 Google Fi)上遇到这个问题。

当我安装应用程序时,SharedPreferences 内有一个 userIsLoggedIn 的密钥。

这个块:

boolean userIsLoggedIn  = SharedPrefs.userIsLoggedIn(this);

// Then in another class...

 public static boolean userIsLoggedIn(Context context) {
    // For users updating apps, if the previous key-value is a string, convert it to boolean
    try {
        return context.getSharedPreferences(LOGIN_FILE, Context.MODE_PRIVATE)
                .getBoolean(USER_LOGGED_IN, false);
    } catch (ClassCastException e) {
        Logger.e(TAG, e.getMessage());
        context.getSharedPreferences(LOGIN, Context.MODE_PRIVATE)
                .edit()
                .putBoolean(USER_LOGGED_IN, false)
                .commit();
        return context.getSharedPreferences(LOGIN, Context.MODE_PRIVATE)
                .getBoolean(USER_LOGGED_IN, false);
    }
}

现在这应该在新卸载时返回false,但在全新安装时调试它我在应用启动时得到以下信息。

如果这很重要,我也会运行 Proguard,当在未启用 proguard 的 APK 上运行设备时,它运行正常。在任何其他设备上运行 proguard 都可以正常运行。

【问题讨论】:

  • 您检查过其他设备吗?代码看起来不错。
  • 我认为 login 在代码中的某处是正确的,因为共享首选项的行为不是这样,所以请先检查一下。
  • 这绝对不是问题,这段代码先运行。已在其他 8 台设备上确认。

标签: android sharedpreferences nexus-6p


【解决方案1】:

由于 Nexus 6P 运行的是 Android M,我认为Automatic Backups 是问题所在。

我认为您可以使用allowBackup 来阻止这种情况。

检查这个答案:https://stackoverflow.com/a/32010582/336312

【讨论】:

    【解决方案2】:

    在 android M 及以上版本中,他们将应用程序备份保存在谷歌驱动程序中,您可以通过使用禁用此功能, 转到 Application 部分下的项目 ma​​nifest 文件,将 android:allowBackup="true" 设置为 false。您可以开始了。

    【讨论】:

      【解决方案3】:

      您可以添加到清单中:

              android:fullBackupContent="false"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-28
        • 2011-06-12
        • 2017-04-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多