简体   繁体   English

Android中的偏好和共享偏好之间的差异

[英]Difference between preference and shared preference in Android

The concept of preferences and shared preferences in Android are mixed up for me. Android中的偏好和共享偏好的概念对我来说很混乱。 What is the difference? 有什么不同?

Preferences : The user interface part of the settings. 首选项 :设置的用户界面部分。 It contains different classes which allow to composes Settings screens from code or XML. 它包含不同的类,允许从代码或XML组合设置屏幕。 They can look like this: 它们看起来像这样:

图片

Shared Preferences : These are used to store values in XML files. 共享首选项 :这些首选项用于在XML文件中存储值。 These files are created, maintained and deleted by Android for you. Android会为您创建,维护和删除这些文件。 They are not encrypted and can easily be changed when the user has rooted his/her phone (nice for development). 它们没有加密,并且当用户植根他/她的手机时很容易更改 (非常适合开发)。 Don't use these for sensitive information. 请勿将这些信息用于敏感信息。 The above mentioned Preferences use Shared Preferences as the underlying system. 上述首选项使用共享首选项作为底层系统。

What the documentation is saying: 文档说的是什么:

  • android.preference : is a package providing classes for preferences management ... The PreferenceScreen contains Preference elements such as a CheckBoxPreference, EditTextPreference, ListPreference, PreferenceCategory, or RingtonePreference... which means that preference is just the UI tools . android.preference :是一个为首选项管理提供类的 ... PreferenceScreen包含Preference元素,如CheckBoxPreference,EditTextPreference,ListPreference,PreferenceCategory或RingtonePreference ......这意味着首选项只是UI工具

  • All settings made for a given Preference will be automatically saved to the application's instance of SharedPreferences . 为给定首选项创建的所有设置将自动保存到应用程序的SharedPreferences实例中。 Access to the SharedPreferences is simple with getSharedPreferences()... which means that this is the way to save these preferences ... 使用getSharedPreferences()可以轻松访问SharedPreferences ...这意味着这是保存这些首选项的方法 ...

To understand it in a simple way- 要以简单的方式理解它 -

SharedPreferences is an interface that manages a set of Preference s. SharedPreferences是一个管理一组Preference的接口。 SharedPreferences are stored as key-value pairs and updated in memory as user interacts with them. SharedPreferences存储为键值对,并在用户与它们交互时在内存中更新。 For eg. 例如。 Brightness is a Preference in Display Settings. 亮度是显示设置中的首选项。

To get hold of all Preferences we use SharedPreferences as 要掌握所有首选项,我们使用SharedPreferences作为

 SharedPreferences sharedPreferences = getPreferenceScreen().getSharedPreferences();

whereas to handle a particular Preference we use 而要处理我们使用的特定偏好

Preference p = getPreferenceScreen().getPreference(index);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM