Android data storage-shared preferences

Source: Internet
Author: User

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:

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.