public interface Sharedpreferences
Import:
Android.content.SharedPreferences;
Overiview:
Mainly used to store some of the APK's property data, such as: Window size, volume and other preference data.
Main methods:
Use:
(1) Obtaining an instance of Sharedpreferences via Getsharedpreferences (string name,int mode)
(2) If you need to deposit data, use Sharedpreferences.editor to get an editor using editor.putstring (string key,string value)
Editor.commit () Deposit data
(3) Read the data using the obtained instance. GetString (String key,string "");//second parameter default value
code example:
A) Data deposit
//instantiating a Sharedpreferences object (first step)Sharedpreferences mysharedpreferences= getsharedpreferences ("test", activity.mode_private); //instantiating a Sharedpreferences.editor object (step two)Sharedpreferences.editor Editor =Mysharedpreferences.edit ();//Save the data in a putstring wayEditor.putstring ("name", "Karl"); Editor.putstring ("Habit", "sleep"); //Submit Current Dataeditor.commit ();//To write data successfully using the TOAST information hint boxToast.maketext ( This, "data successfully written to sharedpreferences! ", Toast.length_long). Show ();
b) read out the data
sharedpreferencessharedpreferences= getsharedpreferences ( "test"// String name =sharedpreferences.getstring ("name", """=sharedpreferences.getstring (" Habit ","""/ /toast.maketext (This, "read the data as follows:" + "\ n" + "name:" + name + "\ n" + "habit:" +
android--using Sharedpreference to store data