【问题标题】:Android Local files that were deleted reappear when the app is installed after a Clean在清理后安装应用程序时,被删除的 Android 本地文件重新出现
【发布时间】:2019-08-27 23:39:40
【问题描述】:

我使用 Visual Studio 编写了一个 Xamarin Android 应用程序来创建 Estimates。在不同的时间,我将 Estimate 保存在本地。如果您尝试重新打开 Estimate,它会尝试本地文件,如果它不存在,它会尝试服务器。 将 Estimate 成功保存到服务器后,我会删除本地文件。这是代码。

            string filePath = Path.Combine(Settings.DocumentsPath, fileName);
            JsonSerializer ser = new JsonSerializer();
            StreamWriter opf = new StreamWriter(filePath, false);
            try
            {
                ser.Serialize(opf, obj);
            }
            catch (Exception ex)
            {
                EventLog.Error(ex);
            }
            opf.Close();

Settings.DocumentsPath 设置如下:

   private static string _DocumentsPath = null;
    public static string DocumentsPath
    {
        get
        {
            _DocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            return _DocumentsPath;
        }
    }

当我加载 Estimate 时,我首先检查是否有本地副本,如果存在则加载该副本。否则它会调用服务器。当 Estimate 成功保存到服务器后,我使用以下代码删除本地文件:

    string filePath = Path.Combine(Settings.DocumentsPath, FName);
    if (File.Exists(filePath))
    {
        try
        {
            File.Delete(filePath);
        }
        catch (Exception ex)
        {
            EventLog.Error(ex);
        }
    }

这行得通。后续调用打开 Estimate 会尝试本地文件,但它不存在,因此请求是从服务器发出的。

问题是,在完成所有这些操作后,如果我清理我的项目并再次运行它,本地保存的 Estimate 的旧版本会重新显示。然后,当加载 Estimate 时,它​​会找到 Estimate 的旧本地版本,并且不会向服务器发送请求。我可以从平板电脑上完全卸载该应用程序并重新安装它,旧的本地文件又回来了。除了 Environment.SpecialFolder.Personal 之外,还有更好的地方来存储本地文件吗?如果在部署应用程序进行调试时,Visual Studio 中某处的设置会阻止它恢复这些本地文件?

【问题讨论】:

    标签: android xamarin visual-studio-2019


    【解决方案1】:

    有一个备份设置,您可以在应用程序标签内的 AndroidManifest 中禁用它:

    android:allowBackup="true"

    android:allowBackup 是否允许应用参与 备份和恢复基础设施。如果此属性设置为 false,不会执行应用程序的备份或恢复, 即使通过一个完整的系统备份,否则会导致所有 要通过 adb 保存的应用程序数据。这个的默认值 属性为真。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-14
      • 2016-01-21
      相关资源
      最近更新 更多