gpt4 book ai didi

android - SharedPreferences 与私有(private)文件

转载 作者:太空狗 更新时间:2023-10-29 14:28:07 26 4
gpt4 key购买 nike

在我的应用程序中,我使用 PreferenceActivity 框架来存储持久数据。我的 Intent 是创建多个保存文件,所有这些文件都可以通过 Preference 访问,但一次只能访问一个。

什么时候使用 Context.openFileOutput() 生成的私有(private)文件更好,什么时候使用 SharedPreferences 更好?

编辑
我的数据仅以原始形式存在。

最佳答案

通常,开发人员使用 getDefaultSharedPreferences 的整个应用程序通用的首选项文件。

但是,Android 在Context 中有一个getSharedPreferences(String name, int mode) 方法.您可以使用它来拥有多个首选项文件,在您的情况下 - 通过使用传递到 name 参数中的唯一名称来保存文件。

关于易变性,您可以通过 edit() 获取 Editor 然后调用 commit() 来强制保存首选项。

请务必注意,SharedPreferences 确实会根据 name 共享:

Retrieve and hold the contents of the preferences file 'name', returning a SharedPreferences through which you can retrieve and modify its values. Only one instance of the SharedPreferences object is returned to any callers for the same name, meaning they will see each other's edits as soon as they are made.

关于android - SharedPreferences 与私有(private)文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9815539/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com