What is the difference between Preferences and SharedPreferences in Android?

五迷三道 提交于 2020-01-14 08:58:27

问题


What is the difference between java.util.prefs.Preferences and android.content.SharedPreferences? Looks like they are for similar things - you can put and get a value by a key in both of them, but Preferences looks like something more difficult and belongs more to the OS than to an app.


回答1:


Preferences is a core java class link1

java.util.prefs.Preferences : This class allows applications to store and retrieve user and system preference and configuration data. This data is stored persistently in an implementation-dependent backing store.

SharedPreferences is an android specific interface link2

android.content.SharedPreferences : 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.




回答2:


Preferences: The user interfaces part of the settings. It contains different classes which allow one to compose Settings screens from code or XML.

Shared Preferences: These are used to store values in XML files. These files are created, maintained and deleted by Android for you. They are not encrypted and can easily be changed when the user has rooted his/her phone. Don't use these for sensitive information. The Preferences mentioned above use Shared Preferences as the underlying system.

To get hold of all Preferences, we use SharedPreferences as

SharedPreferences sharedPreferences = getPreferenceScreen().getSharedPreferences();

whereas to handle a particular Preference we use

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



来源:https://stackoverflow.com/questions/41444724/what-is-the-difference-between-preferences-and-sharedpreferences-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!