1, Sharedpreferences is the most easily understood data storage technology in Android, in fact sharedpreferences processing is a key-value (key value pair). Sharedpreferences is often used to store some lightweight data
public void Setsharevalue () {
Instantiate the Sharedpreferences object (the first step) to the name of the local XML file---Test
Sharedpreferences mysharedpreferences = getsharedpreferences ("Test", activity.mode_private);
Instantiate the Sharedpreferences.editor object (step two) Sharedpreferences.editor Editor = Mysharedpreferences.edit ();
Save the data in a putstring way
Editor.putstring ("name", "Tom");
Editor.putstring ("password", "123");
Submit Current Data editor.commit ();
To write data successfully using the TOAST information hint box
Toast.maketext (This, "data successfully written to sharedpreferences! ", Toast.length_long). Show ();
}
2, execute the above code, Sharedpreferences will save this data in the Test.xml file, you can export the file under File Explorer Data/data, and view. So how do you read the data that has been stored? Let's see.
public void Getsharevalue () {
Similarly, an Sharedpreferences object is instantiated before reading sharedpreferences data
Gets the local XML file name---Test
Sharedpreferences sharedpreferences = getsharedpreferences ("Test", activity.mode_private);
Use the GetString method to get value, note that the 2nd parameter is the default value of value
String name = sharedpreferences.getstring ("name", "This value bit default value when name does not exist");
String habit = sharedpreferences.getstring ("password", "Default value when password does not exist");
Display information using the Toast Information prompt box
Toast.maketext (This, "read the data as follows:" + "\ n" + "name:" + name + "\ n" + "Password:" + habit, Toast.length_long). Show (); }
Android app Development sharedpreferences How to use stored data