Android Simple data storage sharedpreferences

Source: Internet
Author: User

Sharedpreferences is a tool class for storing simple data in Android. It can be imagined that it is a small cookie that stores simple data types (boolean, int, float, long, and string) in the application's private directory by means of a key-value pair (data/data/package name/shared_prefs/) The XML file that you define yourself.

1) Save data

// Get Sharedpreferences Object this. Getsharedpreferences ("Shared_file", 0); // Get editable Objects Sharedpreferences.editor Editor = settings.edit (); Editor.putstring ("name", "Kael Chen"); Editor.putint ( "Age",editor.commit () ;

2) Read data

this. Getsharedpreferences ("Shared_file", 0); // The second parameter assigns a default value if it is not saved in Sharedpreferences String name = settings.getstring ("name", "No Name"); int age = settings.getint ("Age", 0);

3) Delete data

// Get Sharedpreferences Object this. Getsharedpreferences ("Shared_file", 0); // Get editable Objects Sharedpreferences.editor Editor = settings.edit ();  Editor.remove ("name");     // editor.clear ();    Editor.commit ();

4) The corresponding Shared_file.xml

<?XML version= ' 1.0 ' encoding= ' utf-8 ' standalone= ' yes '?><Map><stringname= "Name">Kael Chen</string><intname= "Age" />22</int></Map>

Android Simple data storage sharedpreferences

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.