Document directory
I. sharedpreferences application scenarios
Sharedpreferences is used to save parameters;
It is saved as an XML file in the/data/package/shared_prefs directory;
Ii. sharedpreferences core code
1. Save
Sharedpreferences preference = context. getsharedpreferences ("xiazdong", context. mode _); // obtain sharedpreferenceseditor editor = preference. edit (); // get the editor. putstring ("name", "value"); // enter the string-type parameter editor. putint ("name", value); // Insert the integer parameter editor. commit (); // submit
2. Read
Sharedpreferences preference = context. getsharedpreferences ("FILENAME", context. mode_private); string name = preference. getstring ("name", "defaultvalue"); // obtain the string type parameter named name. Otherwise, the value is defavaluvalueint age = preference. getint ("name", defaultvalue );
Because this method is very simple, you do not need to explain it here;
Iii. Internal principles we mentioned earlier that sharedpreferences are stored in the/data/shared_prefs directory in XML format. Here we will verify this. If we execute the following statement:
SharedPreferences preference = context.getSharedPreferences("xiazdong", Context.MODE_PRIVATE);Editor editor = preference.edit();editor.putString("name", "xiazdong");editor.putInt("age", 20);editor.commit();
The XML file is as follows:
<?xml version='1.0' encoding='utf-8' standalone='yes' ?><map><string name="name">xiazdong</string><int name="age" value="20" /></map>