Sharedpreferences of how Android data is stored

Source: Internet
Author: User

The Android platform provides us with a sharedpreferences class, which is a lightweight storage class that is especially suitable for saving software configuration parameters. Use Sharedpreferences to save the data, behind it is the XML file to hold the data, the file is stored in the/data/data/<package name>/shared_prefs directory:

Store:

Sharedpreferences sp=getsharedpreferences ("UItest", activity.mode_append);
Sharedpreferences.editor Editor=sp.edit ();//Get Editor
Editor.putstring ("Name", Edt1.gettext () + "");
Editor.putstring ("Xuehao", Edt2.gettext () + "");
Editor.putstring ("Zhuanye", Edt3.gettext () + "");
LOG.I ("Mainactivity", rb1.ischecked () + "value");
Editor.putboolean ("Xingbie", rb1.ischecked ());
Editor.commit ();

Getsharedpreferences ("name", "model");

The second parameter model has four modes:

Context.mode_private: The default mode of operation, which means that the file is private and can only be accessed by the app itself, in which the contents of the original file are overwritten.

Context.mode_append: The mode checks whether the file exists, appends content to the file, or creates a new file.

Context.mode_world_readable: Indicates that the current file can be read by another application;

Context.mode_world_writeable: Indicates that the current file can be written by another application.

Disadvantages and parameter meanings:

Only these basic types can be stored:

Editor.putstring ("String Name", "String Value");//String

Editor.putboolean ("String name", "Boolean value");//Boolean

Editor.putint ("String name", "int value");//integral type

Editor.putfloat ("String name", "float value");//floating-point

Editor.putlong ("String name", "Long Value");//Long Integer

Editor.putstringset ("String name", "set<string> value");

Get:

Sharedpreferences sp=getsharedpreferences ("UItest", activity.mode_append);
Sp.getstring ("name", null);//The corresponding value is taken out according to the key, the second parameter represents, when the folder does not have this key default return value is what, the current return empty, the other several are the same

Sharedpreferences of how Android data is stored

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.