问题
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