The Android platform provides a sharedpreferences class, which is a lightweight storage solution inside a lightweight application that is especially suited for saving software configuration parameters such as Boolean,int, float,long,string data. Using Sharedpreferences to save data, the essence is to use the XML file storage data, the path is:/data/data/<package name>/shared_prefs. There are two ways to get sharedpreferences:
(1) Call the Getsharedpreferences () of the context object;(2) Call the Activity object's getpreferences ();
two different ways to differentiate:The Sharedpreferences object obtained by invoking the Getsharedpreferences () method of the context object can be shared by other components under the same application. The Sharedpreferences object obtained by invoking the Getpreferences () method of the activity object can only be used in that activity.
(3) the Sharedpreferences () property, which contains four modes.
3.1, Context.mode_private: The default mode of operation, the representative file is a private file, can only be accessed by the application itself, in which the file will overwrite the original file content
3.2, Context.mode_append: This mode will check whether the file exists, the presence of the file to append content, or create a new file.
3.3. Context.mode_world_readable: Indicates that the current file can be read by another application.
3.4. Context.mode_world_writeable: Indicates that the current file can be written by another application.
(4) Save the data to Sharedpreferences
Sharedpreferences preferences=getsharedpreferences ("user", context.mode_private); Editor Editor=preferences.edit (); String name= "Xixi"; String age= "editor.putstring" ("Name", "name"), Editor.putstring ("age", age); Editor.commit ();
(5)getting data from Sharedpreferences
Sharedpreferences preferences=getsharedpreferences ("user", context.mode_private); String name=preferences.getstring ("name", "DefaultName"); String age=preferences.getstring ("Age", "0");
Sharedpreferences functions in Android