Java Android 中使用的 PreferenceManager 和 SharedPreference 类是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22178414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are the PreferenceManager and SharedPreference classes used for in Android?
提问by Schonge
I've come across two classes being used in a tutorial on splash screens PreferenceManager and SharedPreferences. I didn't gain a great deal of knowledge about them from the tutorial though.
我在关于闪屏 PreferenceManager 和 SharedPreferences 的教程中遇到了两个类。虽然我没有从教程中获得很多关于它们的知识。
So can someone explain to me what both classes do or are used for?
那么有人可以向我解释这两个类的用途或用途吗?
采纳答案by Bharat
Preferencesis an Android lightweight mechanism to store and retrieve pairs of primitive data types (also called Maps, and Associative Arrays).
Preferences是一种 Android 轻量级机制,用于存储和检索原始数据类型对(也称为 Maps 和关联数组)。
In each entry of the form the key is a string and the value must be a primitive data type.
在表单的每个条目中,键是一个字符串,值必须是原始数据类型。
WHEN WE NEED THEM:
当我们需要它们时:
PREFERENCES are typically used to keep state information and shared data among several activities of an application.
PREFERENCES 通常用于在应用程序的多个活动之间保存状态信息和共享数据。
Shared Preferences is the storage, in android, that you can use to store some basic things related to functionality, users' customization or its profile.
Shared Preferences 是 Android 中的存储,您可以使用它来存储一些与功能、用户自定义或其配置文件相关的基本内容。
Suppose you want to save user's name in your app for future purposes. You cant save such a little thing in database, So you better keep it saved in your Preferences. Preferences is just like a file , from which you can retrieve value anytime in application's lifetime in a KEY-VALUE pair manner.
假设您想在您的应用程序中保存用户名以备将来使用。你不能把这么小的东西保存在数据库中,所以你最好把它保存在你的首选项中。Preferences 就像一个文件,您可以在应用程序的生命周期中随时以 KEY-VALUE 对的方式从中检索值。
Take another example, If you use whatsapp, we have a wallpaper option there. How the application knows which image serves as wall-paper for you whenever you open your whatsapp. This information is stored in preferences. Whenever you clear data for any app, preferences are deleted.
再举一个例子,如果你使用 whatsapp,我们在那里有一个壁纸选项。每当您打开 whatsapp 时,应用程序如何知道哪个图像可以作为您的墙纸。此信息存储在首选项中。每当您清除任何应用程序的数据时,首选项都会被删除。
HOW TO USE THESE PREFERENCES :
如何使用这些首选项:
final int mode = Activity.MODE_PRIVATE;
final String MYPREFS = "MyPreferences_001";
// create a reference to the shared preferences object
SharedPreferences mySharedPreferences;
// obtain an editor to add data to my SharedPreferences object
SharedPreferences.Editor myEditor;
mySharedPreferences = getSharedPreferences(MYPREFS, 0);
// using this instance you can get any value saved.
mySharedPreferences.getInt("backColor",Color.BLACK); // default value is BLACK set here
EDITING SHARED PREFERENCES :
编辑共享首选项:
myEditor = mySharedPreferences.edit();
//edit and commit
myEditor.putString("backColor", Color.RED);
myEditor.commit() //very imp.
回答by 1baga
As explained Artoo Detoo... Sharedpreferences kinda works like sessions in web development. you can use them to pass values from one activity to another and it stays that way as far as the app is in use except otherwise changed..
正如 Artoo Detoo 所解释的那样... Sharedpreferences 有点像 Web 开发中的会话。您可以使用它们将值从一个活动传递到另一个活动,并且只要应用程序正在使用,它就会保持这种状态,除非另有更改。
it is also use to user value (either after login or registration of the user). thats how much i can talk about it
它还用于用户价值(在用户登录或注册后)。这就是我可以谈论它的程度
回答by biddulph.r
From the Android Developer site:
从 Android 开发者网站:
Used to help create Preference hierarchies from activities or XML.
Interface for accessing and modifying preference data returned by getSharedPreferences(String, int). For any particular set of preferences, there is a single instance of this class that all clients share.
用于帮助从活动或 XML 创建首选项层次结构。
用于访问和修改由 getSharedPreferences(String, int) 返回的首选项数据的接口。对于任何特定的首选项集,所有客户端都共享此类的单个实例。
Put simply, PreferenceManager
is normally used when you want to create a PreferenceActivity
or load in some Preferences from an .xml
file in your application with default values, and holds it's own referenced to SharedPreferences
.
简而言之,PreferenceManager
当您想使用默认值PreferenceActivity
从.xml
应用程序中的文件创建或加载某些首选项时,通常使用,并保留它自己对SharedPreferences
.
SharedPreferences
is where you handle the storing and retrieving of key/value pairsthat make up your preferences. So you can add variables with keys to retrieve the data later. This feeds into the PreferenceManager
which can handle adding default values and setting up the default SharedPreferences
.
SharedPreferences
是您处理构成您的首选项的键/值对的存储和检索的地方。因此,您可以添加带有键的变量,以便稍后检索数据。这提供给PreferenceManager
可以处理添加默认值和设置默认值的SharedPreferences
.
You can use SharedPreferences
throughout your application without needing to use PreferenceManager
, but the opposite isn't strictly true.
您可以SharedPreferences
在整个应用程序中使用而无需使用PreferenceManager
,但严格来说并非如此。
Further reading:
进一步阅读:
- PreferenceActivity(also PreferenceFragment), which uses
PreferenceManager
in the examples. - Android Data Storagewhich
uses
SharedPreferences
(as well as other options). - Vogella articleon Android Persistence.
- StackOverflow issueon using
SharedPreferences
correctly.
- PreferenceActivity(也是PreferenceFragment),
PreferenceManager
在示例中使用。 - 使用(以及其他选项)的Android数据存储
SharedPreferences
。 - 关于 Android 持久性的Vogella 文章。
SharedPreferences
正确使用的StackOverflow 问题。
回答by rupj
SharedPreferenceAPIs are used to save key-value pairs. They store them in files and are private or public based on the mode you instantiate the SharedPreference object. They are used to store a small set of key-value pairs. This key here is of type String and the value can be any primitive type.
SharedPreferenceAPI 用于保存键值对。它们将它们存储在文件中,并且根据您实例化 SharedPreference 对象的模式是私有的还是公共的。它们用于存储一小组键值对。这里的键是 String 类型,值可以是任何原始类型。
PreferenceManageris part of the Preference APIs. Preference API allows you to define a complete settings UI. This settings UI is an XML layout. You use a PreferenceManager to manage this Preference object's tree. It uses the SharedPreference APIs to store the various settings a user might change using that graphical layout you created.
PreferenceManager是 Preference API 的一部分。Preference API 允许您定义完整的设置 UI。此设置 UI 是一个 XML 布局。您使用 PreferenceManager 来管理此 Preference 对象的树。它使用 SharedPreference API 来存储用户可能使用您创建的图形布局更改的各种设置。
Reference - "Android Docs Training"
参考 - 《Android Docs 培训》