Sharedpreferences of android-data storage

Source: Internet
Author: User

Data storage: Sharedpreferences

I. Basic outline

1. Description

1> is mainly used for storing single small data;

2> Storage Type: Boolean, float, String, long, int

3> Data Save path:/data/data/packagename/shared_prefs/xxx.xml

4> This data is removed when the app is uninstalled (actually---all data under the Package name folder will be removed "PackageName" when the app is uninstalled)

5> can set permissions to manipulate data, eg. can only be read by the current app--mode_private (default)

Mode_world_writeable

Mode_world_readable

Mode_multi_process

Permission to manipulate the data can be obtained by viewing the source code, which are written on the source notes;

2, sharedpreferences corresponding SP file interface

---sharedpreferences context.getsharedpreferences (String name,int mode) Gets the object of the SP implementation class

Parameter one: file name (without. XML, Fixed)

Parameter two: Generate file mode, including the above four kinds.

---editor Sp.edit () Get Editor Object

---xxxsp.getxxx (name,defaultvalue) Gets the specified data based on name, if not returned DefaultValue

3. Editor Update SP File interface

---Editor put (name,value) saves a key-value pair (but does not actually save it to the file)

---Editor remove (name)

---Editor commit (), the data is actually saved to the file.

The above method return value is the same as the editor type, you can make a method chain call.

Second, practice

1. Save Data steps:

1> Get SP Object

2> Get Editor Object

3> get the data to be saved

4> using editor to save data

5> Submit

2. Read the data steps:

1> get the key to query the data

2> read value according to key

Sp.getxxx (Key,value);

Third, the important code

Save data:

Sharedpreferences sp=getsharedpreferences ("KK", mode_private);
Editor Ed=sp.edit ();
Method Chain Call
Ed.putstring ("Sunflower", "I am a beautiful sunflower")
. putstring ("Sunflower", "I am still a beautiful sunflower")//cover the previous sentence value
. putstring ("Kankan", "I'm still a beautiful sunflower")
. Putint ("Cheriong", 23)
. putstring ("Casserole Xuan", "not Tasty"). commit ();

Toast.maketext (Mainactivity.this, "saved successfully", Toast.length_short). Show ();

Through the above experiment, each key corresponds to a unique value, and the storage file is as follows:

File storage path:

Read data:

Sharedpreferences of android-data storage

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.