1. Use the getpreferences method to create a file
File Creation Mode: activity. mode_append
If the file already exists, write the data instead of wiping the end of the existing file.
File Creation Mode: mode_private
The default mode. files created in this mode can only be called by applications, that is, private files.
File Creation Mode: activity. mode_world_readable
Allow all other applications to read and create files.
File Creation Mode: activity. mode_world_writeable
All other applications are permitted to write, access, and create files.
2. Save the sharedpreferences configuration value.
Sharedpreferences uistate = getpreferences (0 );
// Get the editing object
Sharedpreferences. Editor editor = uistate. Edit ();
// Add Value
Editor. putboolean ("bmusic", mbmusic );
Editor. putfloat ("argfloat", 1f );
Editor. putint ("arginet", 2 );
Editor. putlong ("arglong", 1313 );
Editor. putstring ("argstr", "string ");
3. Clear the sharedpreferences configuration value
Clear all values: Editor. Clear ();
Clear a value: Editor. Remove ("argstr ");
4. Submit the modification Value
Editor. Commit ();
Editor. Clear ();
5. Get the sharedpreferences configuration value
Sharedpreferences settings = getpreferences (activity. mode_private );
Mbmusic = settings. getboolean ("bmusic", false );
6. Add a configuration change listener
When multiple applications share the configuration, you can add a configuration change listener to monitor whether the configuration has changed.
Settings. registeronsharedpreferencechangelistener (New sharedpreferences. onsharedpreferencechangelistener (){
@ Override
Public void onsharedpreferencechanged (sharedpreferences, string key ){
System. Out. println ("onsharedpreferencechanged ");
}
});
7. view the sharedpreferences configuration file:
For example, the configuration file corresponding to Android app development is located in the following directory: