The Official Android API: SharedPreferences class provides a common framework for you to save and retrieve persistent key-value pairs of raw data types. You can use SharedPreferences to store any raw data: Booleans, floating point values, integer values, long integers, and strings. This data is retained permanently across multiple user sessions (even if your application has been terminated).

SharedPreferences provides a permanent storage of data in XML format for Android applications and uses key-value pairs to store the data. Compared to an Android application, the /data/ data/your_app_package_name/shared_prefs/directory can be accessed by all activities in the same application. Android provides apis to handle this data without requiring the programmer to directly manipulate the files or worry about data synchronization.

SharedPreferences is an interface, and programs cannot create instances of SharedPreferences directly. GetSharedPreferences (String name,int mode) ¶ getSharedPreferences(String name,int mode) ¶

There are two parameters: The first parameter specifies the name of the SharedPreferences file (in XML format), and if a file with that name does not exist one will be created. The second parameter specifies the mode of operation, as follows:

  • MODE_PRIVATE: The default mode of action. Only this application can read or write this SharedPreferences file.
  • MODE_WORLD_READABLE: Other applications can only read this SharedPreferences file and cannot modify it.
  • MODE_WORLD_WRITEABLE: This SharedPreferences file can be read and written by other applications.
  • MODE_MULTI_PROCESS: this mode has been deprecated since Android2.3 and can be omitted.

It’s also worth mentioning some of the main methods of the SharedPreferences.Editor object.

  • Sharedpreferences.editor Clear (): Deletes all data in SharedPreferences.
  • SharedPreferences.Editor putXxx(String key , xxx value): Store data for the specified key into SharedPreferences, where XXX can be a variety of basic types such as Booleant.
  • Sharedpreferences.editor Remove (): Removes the data item corresponding to the specified key in SharedPreferences
  • Boolean commit(): Use this method to commit changes after editing is complete.

Simple: Store private raw data in key-value pairs.

Trial scope: Used to hold a small amount of data in a very simple format, such as various configuration information for the application. Common cases: music switch, user account Settings, user habits Settings, simple extension: determine whether the program is run for the first time (make the Android app boot interface only display once after Android).

Simple use:

1. Store data

Call edit() to get the SharedPreferences.editor object.

Add values using methods such as putBoolean() and putString().

Commit the new value using commit()

2. Fetch data

Call edit() to get the SharedPreferences.editor object.

Use the SharedPreferences methods such as getBoolean() and getString() to fetch the value.

To read SharedPreferences of other applications:

Be sure that the mode of operation of the application being read is readable!!