- The role of Sharedpreferences
Storage of temporary data, to ensure that the next time access to the system, to obtain the data of the file, to operate.
- Sharedpreferences the process of storing data and reading data:
Storage data Information 1) Open a configuration file named Config, open it if it exists, or create a new configuration file named configuration &N Bsp sharedpreferences sharedpreferences = getsharedpreferences ("Configuration", 0) ; getsharedpreferences (first parameter, second parameter); &NB Sp FIRST parameter: Storage file name second parameter: Open mode can be generated by the context call mode 2) Keep sharedpreferences in edit Editor editor = Sharedpreferences.edit (); 3) store data editor.putstring (" Name "," Harvey "); 4) Complete the submission editor.commit (); Read data information 1) Open config file named configuration sharedpreferences Sharedpreferences = getsharedpreferences ("Configuration", 0); 2) Get data string name = sharedpreferences.getstring ("name", "Default value");
sharedpreferences pre = context.getsharedpreferences (String name,int mode);name is the configuration file name for this component (if you want to share this profile with other components of the application, you can use this name to retrieve this profile, which you should pay special attention to, because the sharedpreferences has been identified in Android is saved as XML, so when you fill in the file name parameter, do not give the ". xml" suffix, and Android will automatically add it. Just write the file name directly. It is stored directly under the/data/data/<package name>/shared_prefs path, which is a key-value pair that holds the parameters. When you need to get a parameter value, follow the key index of the parameter.
How to use Sharedpreferecences in Android