删除 Android 中指定的首选项文件

发布于 2024-09-30 17:58:34 字数 904 浏览 0 评论 0原文

我可以通过调用 Context.getSharedPreferences(String name, int mode),将一些值放入返回的首选项编辑器中,然后提交。但是,当我不再需要创建的文件时,我在 API 中没有看到用于删除它的接口。我试过 Context.deleteFile(String name),传入与上面相同的 name 参数,但返回 false,表明该文件实际上并未被删除。我正在使用 MODE_PRIVATE,如果这有什么区别的话。

浏览源代码 编辑器的上下文实现也没有透露任何线索。

有什么想法吗?这里的背景是我允许我的用户保存他们的“当前”首选项的副本,以便他们可以恢复以前保存的设置。当他们不再需要这些设置时,我想删除保存的首选项文件,这样它就不会占用空间。目前我能做的最好的事情就是对文件进行 edit().clear().commit ,我相信这会删除大部分内容。不过还不如删除。

I can create a new preferences file by calling Context.getSharedPreferences(String name, int mode), putting some values in the returned preferences' editor, and committing. However, I don't see an interface in the API to remove the created file when I no longer need it. I've tried Context.deleteFile(String name), passing in the same name argument from above, but that returns false, indicating the file wasn't actually deleted. I'm using MODE_PRIVATE, if that makes a difference.

Browsing the source for context's implementation of the editor doesn't reveal any clues either, as far as I can tell.

Any ideas? The background here is that I'm allowing my users to save copies of their "current" preferences so that they can restore previously saved settings. When they don't want those settings anymore, I want to remove the saved preferences file so it doesn't take up space. At the moment the best I can do is an edit().clear().commit on the file, which I believe will remove most of the contents. Not as good as delete, though.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秉烛思 2024-10-07 17:58:34

我尝试过 Context.deleteFile(String name),从上面传递相同的名称参数,但返回 false,表明文件实际上并未被删除。

那是因为该文件不在该目录中。

这里的背景是,我允许我的用户保存其“当前”首选项的副本,以便他们可以恢复以前保存的设置。

你如何制作副本?

从支持的 API 的角度来看,最好将真正的 SharedPreferences 设置为当前活动设置,并在您自己的目录中将备份和恢复为您自己的文件格式。

如果您不愿意朝这个方向走,该文件将是相对于 getFilesDir()../shared_prefs 目录中的 XML 文件,因此您可以构造一个适当的文件对象并以这种方式删除它。但是,请记住,Android 稍后可能会更改此位置,甚至更改存储模型,因此您将来可能会遇到兼容性问题。

I've tried Context.deleteFile(String name), passing in the same name argument from above, but that returns false, indicating the file wasn't actually deleted.

That's because the file is not in that directory.

The background here is that I'm allowing my users to save copies of their "current" preferences so that they can restore previously saved settings.

How are you making the copy?

From the standpoint of supported APIs, you are best served by making the real SharedPreferences be the current active settings, and do your backups and restores to your own file format in your own directory.

If you are disinclined to go in that direction, the file will be an XML file in the ../shared_prefs directory relative to getFilesDir(), so you can construct an appropriate File object and delete it that way. However, bear in mind that Android could conceivably change this location later, or even change the storage model, and so you may run into compatibility problems in the future.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文